我的系统中有两个微调器。现在我必须根据第一个微调器值更改第二个微调器的选定值。一旦用户更改第一个微调器值,第二个微调器值将根据第一个微调器的选定值自动设置。如何实现?
最佳答案
来自 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/