如何从Spinner中选择项目,然后单击一下按钮?

时间:2019-02-13 04:45:53

标签: android-studio android-spinner buttonclick

我有3个项目的spinner,并且我想为每个选定的项目做不同的事情,例如,如果选定的项目是“添加”,然后通过单击按钮添加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) {
                }

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

根据我的理解,您希望单击按钮上的选定微调器项目,这样您就可以直接由微调器获得所选项目,而无需任何侦听器。

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

答案 1 :(得分:0)

请勿将spinner.setOnItemSelectedListener写在button.setOnClickListener内。

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

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

这是代码-

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