我想知道同时启用了多少个Switch并打印它们的值

时间:2019-01-22 08:46:28

标签: android android-studio

我想知道同时启用了多少个Switch并打印它们的值 在Toast中,我该怎么做

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CompoundButton;
    import android.widget.Switch;
    import android.widget.Toast;

    public class ListItem extends AppCompatActivity {


        Switch s1,s2,s3,s4;
        int Switch;

        Switch[] switches = new Switch[]{s1,s2,s3,s4};
        Button submit;



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_item);

            s1 = findViewById(R.id.switch1);
            s2 = findViewById(R.id.switch2);
            s3 = findViewById(R.id.switch3);
            s4 = findViewById(R.id.switch4);
            submit = findViewById(R.id.clickbtn);


            s1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                    if (isChecked == true) {


                        String result1 = "Table";







                    }
                }
            });


            s2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                    if (isChecked == true) {

                        String result1 = "Chair";






                    }
                }
            });


            s3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                    if (isChecked == true) {

                        String result1 = "Laptop";



                    }
                }
            });


            s4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


                    if (isChecked == true) {

                        String result1 = "Desktop";






                    }
                }
            });



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


                    for (int i = 1; i <= switches.length; i++) {
                        s








                      //  Toast.makeText(getApplicationContext(), "You picked Table and chair", Toast.LENGTH_LONG).show();


                    }
                }





            });







        }

 }

如果用户检查了三个开关,则我有一些项目的列表,然后用户收到了一个Toast消息,其值与您检查的三项一样,请找到我尝试的代码,但我只有静态方法才能检查这些可能性。

2 个答案:

答案 0 :(得分:1)

不需要setOnCheckedChange侦听器。尝试这个。或根据需要自定义它

Brand   1_day_growth    1_week_growth   1_month_growth  2_week_growth   6_week_growth   1_yr_growth
ABC     10      12      18
ACD     12      18      16
BCD     -       10      14      12      16
BDE     -       12      16      14      13
CDE     -       -       -       -       -       9

答案 1 :(得分:0)

您需要做的是检查提交onClickListener的{​​{1}}上的3开关状态。如果开关已经是Button,则将其文本包含在Toast的字符串中。您已经在做了。只需继续执行代码即可。