微调器选择不会填充edittext

时间:2015-03-26 01:55:12

标签: java android spinner

我有一个功能,当单击一个按钮时,某个变量将根据微调器的选择输入一个edittext的内容。它运作良好。

我正在尝试使用onSelectedItem来填充edittext,而不必单击按钮但它不能正常工作

    Spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


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

            String selection = String.valueOf(Spinner.getSelectedItem());
            if (selection.equals("test")) {
                editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
            } else if (selection.equals("dog")) {

                editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
            } else if (selection.equals("cat")) {
                editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);

            }
        }

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

        }
    });
    }

1 个答案:

答案 0 :(得分:0)

您确定要将对象用于您的小部件Spinner吗?尝试将其更改为:

    Spinner spinner=findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


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

        String selection = spinner.getSelectedItem().toString();
        if (selection.equals("test")) {
            editText3.setText("" + testvalue + "", TextView.BufferType.EDITABLE);
        } else if (selection.equals("dog")) {

            editText3.setText("" + dogvalue + "", TextView.BufferType.EDITABLE);
        } else if (selection.equals("cat")) {
            editText3.setText("" + catvalue + "", TextView.BufferType.EDITABLE);

        }
    }

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

    }
});
}