值变化微调器

发布于 2024-10-20 19:26:01 字数 91 浏览 5 评论 0原文

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

I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花辞树 2024-10-27 19:26:01

来自 Hello Spinner 教程:

现在创建一个实现 AdapterView.OnItemSelectedListener 的嵌套类。这将提供一个回调方法,当从 Spinner 中选择某个项目时,该方法将通知您的应用程序。这个类应该是这样的:

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.
    }
}

AdapterView.OnItemSelectedListener 需要 onItemSelected() 和 onNothingSelected() 回调方法。前者在选择 AdapterView 中的项目时调用,在这种情况下,一条简短的 Toast 消息会显示所选文本;当选择从 AdapterView 中消失时调用后者,在这种情况下不会发生,因此它被忽略。
现在需要将 MyOnItemSelectedListener 应用于 Spinner。返回到 onCreate() 方法并将以下行添加到末尾:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

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

From the Hello Spinner tutorial:

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());

In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.

甜妞爱困 2024-10-27 19:26:01

您必须将条件放在第一个微调器的 onItemSelected 上。通过此示例,您可以根据第一个旋转器获得第二个旋转器的值:

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        if(arg0.equals(spin0)){
        spin1.setClickable(true);
                    if(spin0.getSelectedItem().equals("India"))
                    {
                        ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
                        s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s1);
                    }
                    else  if(spin0.getSelectedItem().equals("Pakistan"))
                    {
                        ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
                        s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s2);
                    }
                    else  if(spin0.getSelectedItem().equals("China"))
                    {
                        ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
                        s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s3);
                    }

        }

    }    

You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

        if(arg0.equals(spin0)){
        spin1.setClickable(true);
                    if(spin0.getSelectedItem().equals("India"))
                    {
                        ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
                        s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s1);
                    }
                    else  if(spin0.getSelectedItem().equals("Pakistan"))
                    {
                        ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
                        s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s2);
                    }
                    else  if(spin0.getSelectedItem().equals("China"))
                    {
                        ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
                        s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spin1.setAdapter(s3);
                    }

        }

    }    
酒浓于脸红 2024-10-27 19:26:01

您应该为每个微调器单独定义 onItemSelected()。否则,如果从任一微调器中选择了任何内容,则代码将被执行。

    newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            String selCat = newCategory.getItemAtPosition(arg2).toString();

            if (selCat != "New")
            {
                loadSpinnerData(topic);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
    newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            // TODO Auto-generated method stub
            loadSpinnerData()
    }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.

    newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub

            String selCat = newCategory.getItemAtPosition(arg2).toString();

            if (selCat != "New")
            {
                loadSpinnerData(topic);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
    newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            // TODO Auto-generated method stub
            loadSpinnerData()
    }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文