简体   繁体   English

将OnFocusChangeListener和OnItemSelectedListener应用于Spinner

[英]Applying OnFocusChangeListener and OnItemSelectedListener to Spinner

I have a spinner that works fine with the standard OnItemSelectedListener to present a set of options: 我有一个微调器,可以与标准OnItemSelectedListener配合使用,以显示一组选项:

s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int selectedPosition, long arg3) {

        if (selectedPosition == 0) {
            // do stuff
        } else {
            // other stuff
        }
    }

    public void onNothingSelected(AdapterView<?> arg0) {
    }
});

However, I have a new requirement that when the user clicks on the Spinner to use it, a corresponding Button is disabled. 但是,我有一个新的要求,即当用户单击微调器以使用它时,将禁用相应的按钮。 I have added the following OnFocusChangeListener to the Spinner: 我已将以下OnFocusChangeListener添加到微调器中:

s.setFocusable(true);
s.setFocusableInTouchMode(true);
s.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            distanceButton.setEnabled(false);
        } else {
            distanceButton.setEnabled(true);
        }
    }

});

This works, and disables the button. 这有效,并禁用该按钮。 HOWEVER, the spinner now need TWO clicks to bring up the options, when without the OnFocusChangeListener it needed only one click. 但是,微调器现在需要单击两次才能显示选项,而如果没有OnFocusChangeListener,则只需单击一下即可。 Is there any way to combine both operations into one Listener? 有什么方法可以将两个操作组合到一个侦听器中?

Thanks... 谢谢...

Try to call super.onFocusChange(v, hasFocus) after your if-clause. 尝试在if子句之后调用super.onFocusChange(v,hasFocus)。

 public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            distanceButton.setEnabled(false);
        } else {
            distanceButton.setEnabled(true);
        }
        super.onFocusChange(v, hasFocus);
    }

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

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