public SeekBar(Context context) | 构造 | 创建SeekBar类的对象 |
public void setOnSeekBarChangeListener( SeekBar.OnSeekBarChangeListener l) | 普通 | 设置改变监听操作 |
public synchronized void setMax(int max) | 普通 | 设置增长的最大值 |
java文件
package com.example.seekbar;import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;public class MainActivity extends Activity implements OnSeekBarChangeListener{private TextView textView1,textView2;private SeekBar seekBar1,seekBar2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView1=(TextView)this.findViewById(R.id.textView1);textView2=(TextView)this.findViewById(R.id.textView2);seekBar1=(SeekBar)this.findViewById(R.id.seekBar1);seekBar2=(SeekBar)this.findViewById(R.id.seekBar2);seekBar1.setOnSeekBarChangeListener(this);seekBar2.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("当前seekbar1刻度"+position);}else {textView2.setText("当前seekbar2刻度"+position);}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("開始滑动seek1");}else {textView2.setText("開始滑动seek2");}}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==seekBar1.getId()){textView1.setText("停止滑动seek1");}else {textView2.setText("停止滑动seek2");}}}
效果图
JAVA文件
package com.example.seekbardemo;import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;public class MainActivity extends Activity implements OnSeekBarChangeListener {
private SeekBar myseekBar;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myseekBar=(SeekBar)this.findViewById(R.id.seekBar1);myseekBar.setOnSeekBarChangeListener(this);}@Overridepublic void onProgressChanged(SeekBar seekBar, int position, boolean flag) {// TODO Auto-generated method stub}
//调节亮度的方法private void setScreenBrightness(float num) {WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // 取得window属性layoutParams.screenBrightness = num; // num已经除以100super.getWindow().setAttributes(layoutParams); // 0~1之间}@Overridepublic void onStartTrackingTouch(SeekBar arg0) {// TODO Auto-generated method stub}//在拖动结束是使用getProgress获得当前的Progress值来设置亮度@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stubif (seekBar.getId()==myseekBar.getId()) {
// 将progress除以100并转换为float类型setScreenBrightness((float)seekBar.getProgress()/100);}}}
效果图