【问题标题】:How to dynamically change the text box value according to the recently selected spinner value?如何根据最近选择的微调器值动态更改文本框值?
【发布时间】:2018-02-19 14:07:12
【问题描述】:

Here is screenshot ...my changes in spinner value reflects only after 2 swipes 我有一个有 10 个屏幕的应用程序,我的主要活动中有 1 个微调器,片段中有一个文本框。我想在我的文本框中显示最近选择的微调器值。

我编写了一个代码来在我的文本框中显示选定的微调器值,并且它在我的所有屏幕中都变得稳定。但我不知道如何根据最近选择的微调器值动态更改文本框值。 (在投反对票之前,请给我一些解决方案

这是获取微调器值的代码

String pos = (String) spinner.getSelectedItem();
        SharedPreferences sharedPref = getSharedPreferences("Mode", Activity.MODE_PRIVATE);
        SharedPreferences.Editor prefEditor = sharedPref.edit();
        prefEditor.putString("userChoicemode", pos);
        prefEditor.commit();

这是检索它的代码

TextView modeselect = (TextView) view.findViewById(R.id.pass);

        SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Mode", Activity.MODE_PRIVATE);
        String get = sharedPref.getString("userChoicemode",selected);

        modeselect.setText("" + get);

【问题讨论】:

  • 只调用spinner的onselecteditem方法
  • 你能解释一下它是如何工作的..??动态的..??
  • 我已经在回答中解释过了,请仔细阅读

标签: java android dynamic spinner


【解决方案1】:

调用下面的方法

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
       String text=spinner.getSelectedItem().toString();
       txtview.setText(text);
                    }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

当列表设置为您的微调器并且每当用户从微调器中选择任何项目时,上述方法都会调用,您也可以通过调用 getSelectedItem() 从按钮单击中获取微调器值,或者如果您只想在用户从中选择项目时获取文本微调器你必须调用上面的覆盖方法

【讨论】:

  • 等哥我试试
  • 它运作良好,但只有一个prblm。它仅在 2 次滑动后更改其状态
  • @Bala_Poyyamoli 请你简要解释一下谁的状态发生了变化?和谁刷卡??
  • 正如我在我的问题中所说,每当我更改微调器值时,它会在更多屏幕的文本框中反映在滑动时的帮助。但文本框的状态仅在 2 次滑动后才会改变。这就是我的 prblm
  • 我不明白你的意思兄弟@Bala_Poyyamoli,请你显示图像描述的问题
【解决方案2】:

试试这个

yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    String selectedText = parent.getItemAtPosition(position).toString()

                    // Set your TextView here
                    myTextView.setText(selectedText);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

如果您的 TextView 位于不同的 Fragment 中,请点击此链接 Communication between Activity and Fragment

【讨论】:

  • 等待库努兄弟我会尝试
  • 它运作良好,但只有一个prblm。它仅在 2 次滑动后更改其状态
【解决方案3】:

您可以拨打spinner的setOnItemSelectedListener

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

                 modeselect.setText(spinner.getSelectedItem().toString());
                 //or 
                 //modeselect.setText(adapterView.getItemAtPosition(i));

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });

【讨论】:

  • 等哥我试试
  • 它运作良好,但只有一个prblm。它仅在 2 次滑动后更改其状态
  • 我该如何纠正它??
  • 您是否使用视图寻呼机进行片段??
【解决方案4】:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

             textView.setText(spinner.getSelectedItem().toString());


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多