怎样使用按钮来监听seekbar-android开发问答
2个回答
展开全部
SeekBar.setOnSeekBarChangeListener(listener);写一个SeekBar改变的监听器,当SeekBar的Progress改变时,监听器触发,在listener内写TextView的SetText
public class TestSeekbar extends Activity {
/** Called when the activity is first created. */
TextView textView = null;
SeekBar seekBar = null;
Button button1 = null, button2 = null;
int p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)this.findViewById(R.id.textview);
seekBar = (SeekBar)this.findViewById(R.id.seekbar);
button1 = (Button)this.findViewById(R.id.button1);
button2 = (Button)this.findViewById(R.id.button2);
seekBar.setOnSeekBarChangeListener(sListener);
button1.setOnClickListener(bListener);
button2.setOnClickListener(bListener);
p = seekBar.getProgress();
}
Button.OnClickListener bListener = new Button.OnClickListener(){
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
p -= 5;
if(p < 0)
p = 0;
seekBar.setProgress(p);
break;
case R.id.button2:
p += 5;
if(p > 100)
p = 100;
seekBar.setProgress(p);
break;
}
}
};
SeekBar.OnSeekBarChangeListener sListener = new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
// TODO Auto-generated method stub
textView.setText("" + progress);
p = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
};
}
public class TestSeekbar extends Activity {
/** Called when the activity is first created. */
TextView textView = null;
SeekBar seekBar = null;
Button button1 = null, button2 = null;
int p;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)this.findViewById(R.id.textview);
seekBar = (SeekBar)this.findViewById(R.id.seekbar);
button1 = (Button)this.findViewById(R.id.button1);
button2 = (Button)this.findViewById(R.id.button2);
seekBar.setOnSeekBarChangeListener(sListener);
button1.setOnClickListener(bListener);
button2.setOnClickListener(bListener);
p = seekBar.getProgress();
}
Button.OnClickListener bListener = new Button.OnClickListener(){
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
p -= 5;
if(p < 0)
p = 0;
seekBar.setProgress(p);
break;
case R.id.button2:
p += 5;
if(p > 100)
p = 100;
seekBar.setProgress(p);
break;
}
}
};
SeekBar.OnSeekBarChangeListener sListener = new SeekBar.OnSeekBarChangeListener(){
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser)
{
// TODO Auto-generated method stub
textView.setText("" + progress);
p = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
};
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询