常见控件4-拖动条(SeekBar)
拖动条(SeekBar) SeekBar.getProgress()获取拖动条当前值 调用setOnSeekBarChangeListener()方法,处理拖动条值变化事件,把SeekBar.OnSeekBarChangeListener实例作为参数传入. |
1、布局: <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SeekBar
android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/seekBarButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取值" />
</LinearLayout>
|
2、核心代码: package cn.zengfansheng.seekbar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private SeekBar seekBar;
private Button seekBarButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) this.findViewById(R.id.seekBar);
seekBarButton = (Button) this.findViewById(R.id.seekBarButton);
seekBar.setMax(100);
seekBar.setProgress(50);
// 监听seekbar状态的改变
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) { // 1、开始拖拽,参数:seekbar表示当前在开始拖拽的SeekBar
Log.i(TAG, "onStartTrackingTouch():"+String.valueOf(seekBar.getProgress()));
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, // 2、拖拽过程中,参数1:当前正在拖拽的SeekBar,参数2:当前进度值,参数3:值变化是否通知
boolean fromUser) {
Log.i(TAG, "onProgressChanged():"+String.valueOf(progress) + ", " + String.valueOf(fromUser));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) { // 3、停止拖拽,参数:seekbar表示当前在停止拖拽的SeekBar
Log.i(TAG,"onStopTrackingTouch():"+ String.valueOf(seekBar.getProgress()));
}
});
seekBarButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "当前seekbar的值:"+seekBar.getProgress(), 0).show();
}
});
}
} |
3、结果: |