Android开发手记(11) 滑动条SeekBar
安卓滑动条的操作特别简单,通过getProgress()可以获得SeekBar的位置,通过setProgress(int progress)可以设置SeekBar的位置。要想动态获取用户对SeekBar的操作的话,只需要设置setOnSeekBarChangeListener即可。
setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
示例代码如下:
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.widget.SeekBar; 4 import android.widget.TextView; 5 6 public class MainActivity extends AppCompatActivity { 7 8 private SeekBar seekBar; 9 private TextView textView; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 seekBar = (SeekBar) findViewById(R.id.seekBar); 17 textView = (TextView) findViewById(R.id.textView); 18 19 seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 20 @Override 21 public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 22 textView.setText(i + " progress"); 23 } 24 25 @Override 26 public void onStartTrackingTouch(SeekBar seekBar) { 27 28 } 29 30 @Override 31 public void onStopTrackingTouch(SeekBar seekBar) { 32 33 } 34 }); 35 } 36 }