2014-01-20 54 views
1
package com.example.des; 

import com.example.des.Question1; 
import com.example.des.R; 


import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.Toast; 

public class Question1 extends Activity implements OnClickListener{ 

    CheckBox q1; 
    CheckBox q2; 
    CheckBox q3; 
    CheckBox q4; 
    Button btndone; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.question); 


     q1 = (CheckBox)findViewById(R.id.q1a); 
     q2 = (CheckBox)findViewById(R.id.q2a); 
     q3 = (CheckBox)findViewById(R.id.q3a); 
     q4 = (CheckBox)findViewById(R.id.q4a); 

     btndone = (Button) findViewById(R.id.done); 
     btndone.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 

     if (q1.isChecked() && q2.isChecked()) { 
      new AlertDialog.Builder(this).setMessage(R.string.positive).show();   
     } 

     if (q3.isChecked() && q4.isChecked()) { 
      new AlertDialog.Builder(this).setMessage(R.string.negative).show();   
     } 

    } 
} 

如果q1和q2是检查,那么结果必须是正的,那么如果q3和q4那么它肯定是负的。但是当我检查q1,q2和q3时,它也变为正值,它不应该给出result..when它不仅Q1和Q2检查..这是什么缺少的代码。?

回答

0

你提到了这个问题,如果1,2和3被选中,但我会认为这也会是一个问题,如果2,3和4被检查,或类似的东西。您将需要更改两个if语句。

// if 1 and 2 are checked, but 3 and 4 aren't 
if (q1.isChecked() && q2.isChecked() && !q3.isChecked() && !q4.isChecked()) { 
    new AlertDialog.Builder(this).setMessage(R.string.positive).show();   
} 

// otherwise, if 3 and 4 are checked, but 1 and 2 aren't 
else if (q3.isChecked() && q4.isChecked() && !q1.isChecked() && !q2.isChecked()){ 
    new AlertDialog.Builder(this).setMessage(R.string.negative).show();   
} 

您也可以使用else if而非if的第二个条件(它们都不可能发生)。

+0

是的,它的工作..很多.. .. – user3085149

+0

其实..它是一个专家系统..所以我会用很多的条件.. ..也许高达q9 ..及其约800 +组合 – user3085149

4

希望我理解你的权利:

if (q1.isChecked() && q2.isChecked() && !q3.isChecked() && !q4.isChecked()) { 
    new AlertDialog.Builder(this).setMessage(R.string.positive).show();   
} 

如果q1q2检查和其他两个不是只会工作。

+0

是的..谢谢你..也许是解决方案.. – user3085149