简体   繁体   English

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

[英]How do I determine what android widget is calling my function?

I have several SeekBar s in an Android app that do practically the same thing (set bass, treble, volume). 我在Android应用程序中有几个SeekBar ,它们实际上做同样的事情(设置低音,高音,音量)。 To save typing out new local classes for OnSeekBarChangeListener per SeekBar , I tried to make a single class that in its onStopTrackingTouch would determine which widget was calling it, and do the proper action. 为了避免为每个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);
    }

}

How do I figure out which widget is calling the onStopTrackingTouch ? 如何确定哪个小部件正在调用onStopTrackingTouch Or is there a cleaner or better way of doing this? 还是有一种更清洁或更佳的方式来做到这一点?

100 % you can determine like this; 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;
                }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM