【问题标题】:How to set onChecked Condition on CheckBoxes [duplicate]如何在复选框上设置 onChecked 条件 [重复]
【发布时间】:2018-02-13 14:25:51
【问题描述】:

我有两个 CheckBox 让我们将其视为 ChckBx1 和 ChckBx2。

现在我需要实现几个方法调用复选框是否选中。

要获得更多权限,如果选中 chckBx1,则调用 Method1,如果选中 ChckBx2,则调用 Method2,如果选中两个 CheckBox,则调用 Method3。

有三种不同的条件。

请提供一些代码或示例。

感谢任何帮助。

【问题讨论】:

    标签: android checkbox conditional-statements


    【解决方案1】:

    先检查两者是否都被检查,如果没有返回并分别检查每个状态:

    if(chckBx2.isChecked() && chckBx1.isChecked()){
     //call method 3
      return;
    }
    if(chckBx2.isChecked()){
      //call method 2
      return;
    }
    if(chckBx1.isChecked()){
      //call method 1
      return;
    }
    

    【讨论】:

      【解决方案2】:

      我们需要在复选框上使用 setOnCheckedChangeListener

      ChckBx1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
             @Override
             public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
               if (isChecked and ChckBx2.isChecked()) {
                   method3();
                }
      
               if (isChecked) {
                   method1();
               }
             }
         }
      ); 
      

          ChckBx2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      
                 @Override
                 public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                   if (isChecked and ChckBx1.isChecked()) {
                       method3();
                    }
      
                   if (isChecked) {
                       method2();
                   }
                 }
             }
          ); 
      

      【讨论】:

      • 正确的解决方案但是为什么要添加两个不同的监听对象
      • @NayanSrivastava,是的,它可以使用一个侦听器对象来完成。只是为了清楚和简单的理解,我给出了两个听众的解决方案。
      【解决方案3】:

      您 onCheckedChangeListener 订阅复选框的状态更改。相同的紧凑和清晰的示例可能是

      final CheckBox checkBox1, checkBox2;
      
              CompoundButton.OnCheckedChangeListener listener=new CompoundButton.OnCheckedChangeListener() {
      
                  @Override
                  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                      if(checkBox1.isChecked()&&checkBox2.isChecked())
                      {
                          // do what you want to do if both are checked
                      }
                      else if(checkBox1.isChecked())
                      {
                          // do what you want to do if only checkbox1 is checked
                      }
                      else if(checkBox2.isChecked())
                      {
                          // do what you want to do if only checkbox2 is checked
                      }
                  }
              };
      

      用各自的 ID 初始化 checkbox1&checkbox2 希望这会对您的情况有所帮助

      【讨论】:

        【解决方案4】:
        void callMethods(Checkbox checkbox1, Checkbox checkbox2){
        if(checkbox1.isChecked() && checkbox2.isChecked()){
           //call method three
        } 
        else if(checkbox1.isChecked()){
            //call method one
          }
        else if(checkbox2.isChecked()){
           //call method two
         }
        }
        

        【讨论】:

          【解决方案5】:

          简单易查

          if(checkBx1.isChecked() && checkBx2.isChecked()){
          //call method three
           return;
          } 
          else if(checkBx1.isChecked()){
           //call method one
           return;
          }
          else if(checkBx2.isChecked()){
          //call method two
           return;
          }  
          

          【讨论】:

            【解决方案6】:

            其实很简单,看看下面的代码你就明白了。

            package com.example.checkbox;  
            
            import android.os.Bundle;  
            import android.app.Activity;  
            import android.view.Menu;  
            import android.view.View;  
            import android.view.View.OnClickListener;  
            import android.widget.*;  
            
            public class MainActivity extends Activity {  
                CheckBox pizza,coffe,burger;  
                Button buttonOrder;  
                @Override  
                protected void onCreate(Bundle savedInstanceState) {  
                    super.onCreate(savedInstanceState);  
                    setContentView(R.layout.activity_main);  
                    addListenerOnButtonClick();  
                }  
            public void addListenerOnButtonClick(){  
                //Getting instance of CheckBoxes and Button from the activty_main.xml file  
                pizza=(CheckBox)findViewById(R.id.checkBox1);  
                coffe=(CheckBox)findViewById(R.id.checkBox2);  
                burger=(CheckBox)findViewById(R.id.checkBox3);  
                buttonOrder=(Button)findViewById(R.id.button1);  
            
                //Applying the Listener on the Button click  
                buttonOrder.setOnClickListener(new OnClickListener(){  
            
                    @Override  
                    public void onClick(View view) {  
                        int totalamount=0;  
                        StringBuilder result=new StringBuilder();  
                        result.append("Selected Items:");  
                        if(pizza.isChecked()){  
                            result.append("\nPizza 100Rs");  
                            totalamount+=100;  
                        }  
                        if(coffe.isChecked()){  
                            result.append("\nCoffe 50Rs");  
                            totalamount+=50;  
                        }  
                        if(burger.isChecked()){  
                            result.append("\nBurger 120Rs");  
                            totalamount+=120;  
                        }  
                        result.append("\nTotal: "+totalamount+"Rs");  
                        //Displaying the message on the toast  
                        Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();  
                    }  
            
                });  
            }
            

            【讨论】:

              猜你喜欢
              • 2018-01-27
              • 1970-01-01
              • 2014-07-20
              • 1970-01-01
              • 2023-03-23
              • 1970-01-01
              • 2011-06-27
              • 1970-01-01
              • 2017-02-18
              相关资源
              最近更新 更多