android - 值(value)变化的微调

标签 android spinner onchange

我的系统中有两个微调器。现在我必须根据第一个微调器值更改第二个微调器的选定值。一旦用户更改第一个微调器值,第二个微调器值将根据第一个微调器的选定值自动设置。如何实现?

最佳答案

来自 Hello Spinner教程:

Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
      Toast.makeText(parent.getContext(), "The planet is " +
          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored. Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end: spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

换句话说,您需要创建一个 OnItemSelectedListener 来修改第二个微调器的值,并将其附加到第一个微调器。

关于android - 值(value)变化的微调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219945/

相关文章:

javascript - 检查复选框是否被选中,返回 addEventListener 不是一个函数

javascript - Firefox中Onchange不改变就触发

java - 在android音板中,如何使一种声音在点击另一种声音后停止播放?

java - 在4列 ListView android中过滤数据

angular - Ng4LoadingSpinner 5 秒后超时

android - 我如何从 JSON 中获取国家和州并将其填充到 Spinner 上

reactjs - 仅在 onChange 停止触发设定时间后,如何使 React 输入 onChange 设置状态?

android - 在刷新期间使用 SwipeRefreshLayout 切换 fragment 时, fragment 卡住但实际上仍然有效

java - 对于旋转 View ,执行(点击())在 espresso ui 测试中不起作用

javascript - React Spinner 在没有状态的情况下加载动画/ View ?