Toast选择了切换按钮

时间:2017-11-18 11:10:43

标签: java android android-togglebutton

我创建了7 ToggleButtonsbutton。每个星期一,星期二,星期天......

当我选择("Switch ON")例如星期一和星期四时,我想触摸一个按钮并显示"您已选择星期一和星期四"。

任何想法如何在MainActivity.java

提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以通过为所有七个切换按钮设置onCheckedChangeListener并将其附加到字符串来解决此问题。点击后,可以显示该字符串。

答案 1 :(得分:0)

  public class MainActivity extends AppCompatActivity {

  private ToggleButton toggleButton_Monday;
  private ToggleButton toggleButton_Thursday,toggleButton_wednesday;
  private Button buttonSubmit;

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

    toggleButton_Monday=(ToggleButton)findViewById(R.id.toggleButton1);
    toggleButton_Thursday=(ToggleButton)findViewById(R.id.toggleButton2);
    toggleButton_wednesday=(ToggleButton)findViewById(R.id.toggleButton3);
    buttonSubmit=(Button)findViewById(R.id.button1);


    buttonSubmit.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            StringBuilder result = new StringBuilder();


            if(toggleButton_Monday.isChecked()) {

                result.append("Monday ");
            }

            if(toggleButton_Thursday.isChecked()) {

                result.append("Thursday ");

            }

            if(toggleButton_wednesday.isChecked()) {

                result.append("Wednesday ");

            }
            //Displaying the message in toast
            Toast.makeText(getApplicationContext(),"You have selected "+result.toString(),Toast.LENGTH_LONG).show();
        }

    });

}
 }

你可以这样做。