向Seekbar添加一个监听器

时间:2018-01-06 16:51:39

标签: android listener seekbar

我正在尝试在android studio中使用Seekbar,以便用户可以输入1-100的某个值。但是,我无法弄清楚如何将监听器添加到Seekbar。

2 个答案:

答案 0 :(得分:1)

以下是如何在搜索栏上设置监听器以进行搜索栏位置更改

 seekBar1.setOnSeekBarChangeListener(this);  
 seekBar1.setMax(1000);
 seekBar1.setProgress(50);

示例代码

   public class MainActivity extends Activity implements OnSeekBarChangeListener{  
    SeekBar seekBar1;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
        seekBar1.setOnSeekBarChangeListener(this); 
        seekBar1.setMax(1000); 
    }  
    @Override  
    public void onProgressChanged(SeekBar seekBar, int progress,  
            boolean fromUser) {  
        Toast.makeText(getApplicationContext(),"seekbar progress: "+progress, Toast.LENGTH_SHORT).show();  
    }  
    @Override  
    public void onStartTrackingTouch(SeekBar seekBar) {  
        Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();  
    }  
    @Override  
    public void onStopTrackingTouch(SeekBar seekBar) {  
        Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
    }  
} 

答案 1 :(得分:0)

如果您不需要实现界面,请使用此

sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {       

@Override       
public void onStopTrackingTouch(SeekBar seekBar) {      
    // TODO Auto-generated method stub      
}       

@Override       
public void onStartTrackingTouch(SeekBar seekBar) {     
    // TODO Auto-generated method stub      
}       

@Override       
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {     
    // TODO Auto-generated method stub      


    Toast.makeText(getApplicationContext(), String.valueOf(progress),Toast.LENGTH_LONG).show();

}