其他分享
首页 > 其他分享> > android studio SeekBar滑动轴显示数字

android studio SeekBar滑动轴显示数字

作者:互联网

1.样式xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <SeekBar
            android:id="@+id/progress"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/text1"
            android:maxHeight="2dp"
            android:minHeight="2dp"
            android:paddingBottom="3dp"
            android:paddingLeft="12dp"
            android:max="30"
            android:min="1"
            android:paddingRight="12dp"
            android:paddingTop="10dp"
            android:progressDrawable="@drawable/layer_progress"
            android:thumb="@drawable/shape_circle" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="left"
            android:padding="6dp"
            android:text="1"
            android:textColor="#00BFFF"
            android:textSize="20dp" />

    </LinearLayout>

2.后台代码

private SeekBar seekBar;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        seekBar = (SeekBar) findViewById(R.id.progress);
        textView = (TextView) findViewById(R.id.text1);
        seekBar.setProgress(15);//设置节点初始位置
        textView.setText("15");//设置节点初始值
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 当拖动条的滑块位置发生改变时触发该方法,在这里直接使用参数progress,即当前滑块代表的进度值
                //textView.setText("Value:" + Integer.toString(progress));
                //Toast.makeText(MainActivity.this, "正在滑动!"+ progress, Toast.LENGTH_SHORT).show();
                textView.setText(Integer.toString(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //Log.e("------------", "开始滑动!");
                Toast.makeText(MainActivity.this, "开始滑动!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //Log.e("------------", "停止滑动!");
                Toast.makeText(MainActivity.this, "停止滑动!", Toast.LENGTH_SHORT).show();
            }
        });
    }

标签:Toast,SeekBar,seekBar,studio,滑动,progress,android,textView
来源: https://blog.csdn.net/qq_35086941/article/details/121228322