如何 select 来自 Spinner 的项目然后在单击按钮时执行某些操作?

How to select item from Spinner then do somthing on button click?

我有 spinner 和 3 个项目,我想为每个选定的项目做不同的事情,比如如果选定的项目是 "add" 然后通过单击按钮添加 2 个数字,我已经实现了一个可以工作的代码,但不能点击按钮我该如何连接它。

这是我的代码。

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

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

                    String uints = parent.getItemAtPosition(position).toString();
                    switch (position) {

                        case 0:
                            double r1 = Double.parseDouble(input1.getText().toString());
                            double r2 = Double.parseDouble(input2.getText().toString());
                            double rout = r1 + r2;
                            textView.setText(Double.toString(rout));
                            break;

                        case 1:

                            double r12 = Double.parseDouble(input1.getText().toString());
                            double r22 = Double.parseDouble(input2.getText().toString());
                            double rout2 = r12 - r22;
                            textView.setText(Double.toString(rout2));

                            break;
                        case 2:
                            double r13 = Double.parseDouble(input1.getText().toString());
                            double r23 = Double.parseDouble(input2.getText().toString());
                            double rout3 = r13 / r23;
                            textView.setText(Double.toString(rout3));
                            break;
                    }
                }

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

感谢您的帮助。

不要在button.setOnClickListener里面写spinner.setOnItemSelectedListener

首先添加spinner.setOnItemSelectedListener并将值存储在任何integer中。

现在比较 button.setOnClickListener 处的 interger 值并执行操作。

这是代码 -

int selectedPosition = 0;
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

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

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


    button.setOnClickListener(new View.OnClickListener()

{
    @Override
    public void onClick (View v){
    switch (selectedPosition) {

        case 0:
            double r1 = Double.parseDouble(input1.getText().toString());
            double r2 = Double.parseDouble(input2.getText().toString());
            double rout = r1 + r2;
            textView.setText(Double.toString(rout));
            break;

        case 1:

            double r12 = Double.parseDouble(input1.getText().toString());
            double r22 = Double.parseDouble(input2.getText().toString());
            double rout2 = r12 - r22;
            textView.setText(Double.toString(rout2));

            break;
        case 2:
            double r13 = Double.parseDouble(input1.getText().toString());
            double r23 = Double.parseDouble(input2.getText().toString());
            double rout3 = r13 / r23;
            textView.setText(Double.toString(rout3));
            break;
    }
}
});

根据我的理解,您希望在单击按钮时选择微调器项目,这样您就可以直接通过微调器获取所选项目而无需任何侦听器。

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               switch(spinner.getSelectedItemPosition()){
                    //Do whatever you want
               }
            }
}

您可以使用默认函数获取所选项目的 ID、视图、位置或项目本身。

spinner.getSelectedItemId()
spinner.getSelectedItemView()
spinner.getSelectedItemPosition()
spinner.getSelectedItem()