2014-02-13 31 views
1

我是Android新手,請幫助我。如何比較android上的兩個複選框值?

如果以下代碼中的兩個複選框都啓用,我該如何實現打印值?

public EditText output; 
Button one; 

int a; 
CheckBox check,check1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    output =(EditText)findViewById(R.id.editText1); 
    one=(Button)findViewById(R.id.button1); 
    check = (CheckBox)this.findViewById(R.id.checkbox1); 
    check1 = (CheckBox)this.findViewById(R.id.checkbox2); 

    check.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
        //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       a=100; 

      } 
      else{ 
       a=0; 
      } 
       } 
         }); 

    check1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
        //is chkIos checked? 
      if (((CheckBox) v).isChecked()) { 
       a=100; 
      } 
      else{ 
       a=0; 
      } 
      } 
}); 


//this statment is wrong. if((check.isChecked() == true)&&(check.isChecked() == true)){ 

     a=200; 
    } 

one.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) {     
      if(v==one){ 
       output.setText(""+a); 
      } 
     } 
}); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

回答

1

的OnClick方法複選框的狀態在按鈕

if((checkBox1.isChecked()) && (checkBox2.isChecked)){ 
    editText.setText("Both are checked"); 
} 

的onclick方法,我們不需要指定任何變量器isChecked方法返回一個布爾值,因此可以在內部直接檢查if。

+0

這些對我的代碼沒有任何影響:( – user3306441

+0

)您是否刪除了您在複選框中調用的setOnClickListener()的實現?是否還可以發佈已做出的更改? –

+0

(R.id.checkbox1); \t \t check1 =(複選框)this.findViewById(R.id.checkbox2); \t \t \t \t one.setOnClickListener(新View.OnClickListener(){ \t \t \t公共無效的onClick(視圖v) \t \t { \t \t \t \t如果(check.isChecked()){A = 100;} \t \t \t \t否則{A = 0;} \t \t \t \t如果(check1.isChecked()){A = 100;} \t \t \t \t否則{A = 0;} \t \t \t 如果\t(check.isChecked()&& check1.isChecked()) \t \t { \t \t \t一個= 200; \t \t} \t \t否則{A = 0;} \t \t如果(V ==一個) \t \t { \t \t \t輸出。的setText( 「」 + A); \t \t \t} \t \t \t \t}}); 這是我的新代碼 – user3306441

0

聲明兩個複選框作爲最終與

if (check.isChecked() == true && check1.isChecked == true) 

對我來說,它的工作原理進行比較。例如:檢查按鈕

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

    final CheckBox check = (CheckBox)findViewById(R.id.check); 
    Button button1 = (Button)findViewById(R.id.button1); 

    button1.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       Log.d("Tag-test", "Value:" + check.isChecked()); 
      } 
    }); 
} 
+0

這不是wroking爲 如果check.isChecked(){//註釋} DONOT返回任何值。 – user3306441

1
public void onCheckboxClicked(View view) { 
    // Is the view now checked? 
    boolean checked = ((CheckBox) view).isChecked(); 

    // Check which checkbox was clicked 
    switch(view.getId()) { 
     case R.id.checkbox_meat: 
      if (checked) 
       // Put some meat on the sandwich 
      else 
       // Remove the meat 
      break; 
     case R.id.checkbox_cheese: 
      if (checked) 
       // Cheese me 
      else 
       // I'm lactose intolerant 
      break; 
     // TODO: Veggie sandwich 
    } 
} 
+0

如果checkbox_meat和checkbox_cheese都被選中,我需要執行一些操作,請幫助我。 – user3306441

+0

你能告訴我你需要在那裏做什麼行動嗎?你需要幫助的是什麼部分的實施? – mayooran

0
This is my new code 

public EditText output; 
Button one; 

int a; 
CheckBox check,check1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
output =(EditText)findViewById(R.id.editText1); 
    one=(Button)findViewById(R.id.button1); 
    check = (CheckBox)this.findViewById(R.id.checkbox1); 
    check1 = (CheckBox)this.findViewById(R.id.checkbox2); 

    one.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) 
     { 
      if(check.isChecked()){a=100;} 
      else{a=0;} 
      if(check1.isChecked()){a=100;} 
      else{a=0;} 

       if(check.isChecked()&& check1.isChecked()) 
       { 
        a=200; 
       } 
       else{a=0;} 
       if(v==one) 
       { 
        output.setText(""+a); 
        } 

     }}); 
+0

here if(check.isChecked()&& check1.isChecked())working if but(check.isChecked()){a = 100;} else {a = 0;} if check1.isChecked()){a = 100;} else {a = 0;} 賦值0 – user3306441