如何确定哪个android小部件正在调用我的函数?

时间:2013-08-24 06:10:50

标签: java android resources seekbar

我在Android应用中有几个SeekBar实际上做同样的事情(设置低音,高音,音量)。为了保存每个OnSeekBarChangeListener SeekBar的新本地类的输入,我试图在其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?或者有更清洁或更好的方法吗?

1 个答案:

答案 0 :(得分:1)

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;
                }
    }