Android:SeekBar用法

作者: android00 发布时间: 2021-01-05 浏览: 2586 次 编辑

Android:SeekBar用法

直接上代码:

// MainActivity.java

package com.example.administrator.seekbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private SeekBar sb1, sb2;
    private SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            Toast.makeText(MainActivity.this, "当前进度:" + progress, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            Toast.makeText(MainActivity.this, "开始:" + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            Toast.makeText(MainActivity.this, "结束:" + seekBar.getProgress(), Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sb1 = (SeekBar)findViewById(R.id.seekBar);
        sb2 = (SeekBar)findViewById(R.id.seekBar2);

        sb1.setOnSeekBarChangeListener(onSeekBarChangeListener);
        sb2.setOnSeekBarChangeListener(onSeekBarChangeListener);
    }
}


xml代码:


<SeekBar

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:maxHeight="2dp"

android:max="100"

android:progress="30"

android:thumb="@drawable/seek_bar_thumb"/>