草庐IT

android - 微调器 :How to know whether item selection was changed programmatically or by a user action through UI

coder 2023-11-19 原文

我有运行 OnItemSelectedListener 微调器事件的代码。所以当我在方法中时:

public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // I want to do something here if it's a user who changed the the selected item
}

...我如何知道项目选择是以编程方式完成的还是通过用户界面通过用户操作完成的?

最佳答案

我不知道这个可以从方法内部区分。事实上,这是一个很多人都面临的问题,当微调器启动时 onItemSelected 被触发。目前看来,唯一的解决方法是为此使用外部变量。

private Boolean isUserAction = false;

...

public void onItemSelected( ... ) {

    if( isUserAction ) {
       // code for user initiated selection
    } else {
       // code for programmatic selection
       // also triggers on init (hence the default false)
    }

    // reset variable, so that it will always be true unless tampered with
    isUserAction = true;
}

public void myButtonClick( ... ) {
    isUserAction = false;
    mySpinner.setSelectedItem ( ... );
}

关于android - 微调器 :How to know whether item selection was changed programmatically or by a user action through UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2833121/

有关android - 微调器 :How to know whether item selection was changed programmatically or by a user action through UI的更多相关文章

随机推荐