我在一个 Android 应用程序中有几个 SeekBar
,它们实际上做同样的事情(设置低音、高音、音量)。为了节省为每个 SeekBar
为 OnSeekBarChangeListener
键入新的本地类,我尝试创建一个类,在其 onStopTrackingTouch
中确定正在调用哪个小部件它,并采取适当的行动。
public class mySeekBar implements SeekBar.OnSeekBarChangeListener {
int progressChanged = 0;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
progressChanged = progress;
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {
// i want a case statement here switched on the widget ID/name, so
// i can set the appropriate string s (bass, treble, volume)
String s = "set_treble " + progressChanged;
client.sendMessage(s);
}
}
我如何确定哪个小部件正在调用 onStopTrackingTouch
?还是有更清洁或更好的方法来做到这一点?
最佳答案
100%你可以这样判断;
public void onStopTrackingTouch(SeekBar seekBar) {
// i want a case statement here switched on the widget ID/name, so
// i can set the appropriate string s (bass, treble, volume)
String s = "set_treble " + progressChanged;
client.sendMessage(s);
switch (seekBar.getId()) {
case R.id.seekVolume:
break;
case R.id.bass:
break;
case R.id.trouble:
break;
default:
break;
}
}
关于java - 如何确定哪个 android 小部件正在调用我的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18415556/