2014-02-11 68 views
1

我已經寫了這段代碼,請告訴我該怎麼做才能使Button執行某些操作,具體取決於選中哪個CheckBox。使一個按鈕做一些事情,取決於哪個CheckBox被選中

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

public void CalcolaC(View view) { 

    double Onorario1,SpeseD1,CostI1,Iva1,Deb1,Rit1,ccp1,Netto1; 
    // 
    Onorario1 = 0; 
    SpeseD1 = 0; 
    ccp1 = 0; 

    // 
    EditText Onorario = (EditText) findViewById(R.id.Onorario); 
    EditText SpeseD = (EditText) findViewById(R.id.SpeseD); 
    TextView CostI = (TextView) findViewById(R.id.CostI); 
    TextView Iva = (TextView) findViewById(R.id.Iva); 
    TextView Deb = (TextView) findViewById(R.id.Deb); 
    TextView Rit = (TextView) findViewById(R.id.Rit); 
    TextView ccp = (TextView) findViewById(R.id.ccp); 
    TextView Netto = (TextView) findViewById(R.id.Netto); 
    final CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1); 
    final CheckBox checkBox2=(CheckBox)findViewById(R.id.checkBox2); 
    // 
    try{ 
     Onorario1 = Math.round(Double.parseDouble(Onorario.getText().toString())*100.0)/100.0; 
     SpeseD1 = Math.round(Double.parseDouble(SpeseD.getText().toString())*100.0)/100.0; 
    } catch (NumberFormatException e){ 
     ErrorMsg(); 
     Pulisci(view); 
     return; 
    } 

    if(checkBox1.isChecked()) 
       ccp1 = Onorario1 * 2/100; 
       Iva1 = (Onorario1 + ccp1)*22/100; 
       Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
       CostI1 = (Onorario1+ccp1+SpeseD1); 
       Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
       Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
       // 
       ccp.setText(Double.toString(round(ccp1,2))); 
       Iva.setText(Double.toString(round(Iva1,2))); 
       Rit.setText(Double.toString(round(Rit1,2))); 
       CostI.setText(Double.toString(round(CostI1,2))); 
       Deb.setText(Double.toString(round(Deb1,2))); 
       Netto.setText(Double.toString(round(Netto1,2))); 
    if(checkBox2.isChecked()) 
       ccp1 = Onorario1 * 4/100; 
       Iva1 = (Onorario1 + ccp1)*22/100; 
       Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
       CostI1 = (Onorario1+ccp1+SpeseD1); 
       Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
       Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
       // 
       ccp.setText(Double.toString(round(ccp1,2))); 
       Iva.setText(Double.toString(round(Iva1,2))); 
       Rit.setText(Double.toString(round(Rit1,2))); 
       CostI.setText(Double.toString(round(CostI1,2))); 
       Deb.setText(Double.toString(round(Deb1,2))); 
       Netto.setText(Double.toString(round(Netto1,2))); 

    } 

private void Pulisci(View view) { 
    EditText Onorario = (EditText) findViewById(R.id.Onorario); 
    EditText SpeseD = (EditText) findViewById(R.id.SpeseD); 
    TextView CostI = (TextView) findViewById(R.id.CostI); 
    TextView Iva = (TextView) findViewById(R.id.Iva); 
    TextView Deb = (TextView) findViewById(R.id.Deb); 
    TextView Rit = (TextView) findViewById(R.id.Rit); 
    TextView ccp = (TextView) findViewById(R.id.ccp); 
    TextView Netto = (TextView) findViewById(R.id.Netto); 
    Onorario.setText(""); 
    SpeseD.setText(""); 
    CostI.setText(""); 
    Iva.setText(""); 
    Deb.setText(""); 
    Rit.setText(""); 
    ccp.setText(""); 
    Netto.setText(""); 

} 

private void ErrorMsg() { 
    AlertDialog Msg = new AlertDialog.Builder(this).create(); 
    Msg.setTitle("Errore"); 
    Msg.setMessage("Hai inserito dei dati non validi!"); 
    Msg.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     }}); 
    Msg.show(); 
} 
public static double round(double value, int places) { 
    if (places < 0) throw new IllegalArgumentException(); 

    long factor = (long) Math.pow(10, places); 
    value = value * factor; 
    long tmp = Math.round(value); 
    return (double) tmp/factor; 
} 

} 

這裏是logcat的

02-11 13:20:11.880: E/AndroidRuntime(549): java.lang.IllegalStateException: Could not find a method onCheckboxClicked(View) in the activity class com.ITE.economiaaziendale.CostiDiImpianto for onClick handler on view class android.widget.CheckBox with id 'checkBox2' 
+0

使用oncheckedchangelistener ... \ – Ranjit

+0

我應該在哪裏放? – user3293120

+0

實現接口,然後重寫方法來偵聽複選框更改,然後在該方法內部執行任何想要的按鈕。 – Eenvincible

回答

1

您可以使用監聽器,這樣的:

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

      } else { 

      } 
     } 
}); 
1

設置setOnClickListener()您checkboxs。

checkBox1.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) {    
     if (((CheckBox) v).isChecked()) { 
        ccp1 = Onorario1 * 2/100; 
        Iva1 = (Onorario1 + ccp1)*22/100; 
        Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
        CostI1 = (Onorario1+ccp1+SpeseD1); 
        Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
        Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
        // 
        ccp.setText(Double.toString(round(ccp1,2))); 
        Iva.setText(Double.toString(round(Iva1,2))); 
        Rit.setText(Double.toString(round(Rit1,2))); 
        CostI.setText(Double.toString(round(CostI1,2))); 
        Deb.setText(Double.toString(round(Deb1,2))); 
        Netto.setText(Double.toString(round(Netto1,2)));  
     } 
     } 
    }); 

checkBox2.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) {    
     if (((CheckBox) v).isChecked()) {   
       ccp1 = Onorario1 * 4/100; 
       Iva1 = (Onorario1 + ccp1)*22/100; 
       Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
       CostI1 = (Onorario1+ccp1+SpeseD1); 
       Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
       Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
       // 
       ccp.setText(Double.toString(round(ccp1,2))); 
       Iva.setText(Double.toString(round(Iva1,2))); 
       Rit.setText(Double.toString(round(Rit1,2))); 
       CostI.setText(Double.toString(round(CostI1,2))); 
       Deb.setText(Double.toString(round(Deb1,2))); 
       Netto.setText(Double.toString(round(Netto1,2))); 
     } 
     } 
    }); 

setOnCheckedChangeListener()

checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked){ 
      ccp1 = Onorario1 * 2/100; 
       Iva1 = (Onorario1 + ccp1)*22/100; 
       Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
       CostI1 = (Onorario1+ccp1+SpeseD1); 
       Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
       Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
       // 
       ccp.setText(Double.toString(round(ccp1,2))); 
       Iva.setText(Double.toString(round(Iva1,2))); 
       Rit.setText(Double.toString(round(Rit1,2))); 
       CostI.setText(Double.toString(round(CostI1,2))); 
       Deb.setText(Double.toString(round(Deb1,2))); 
       Netto.setText(Double.toString(round(Netto1,2)));  
     } 
}); 

checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked){ 
       ccp1 = Onorario1 * 4/100; 
       Iva1 = (Onorario1 + ccp1)*22/100; 
       Rit1 = (Onorario1+ccp1+Iva1+SpeseD1)*20/100; 
       CostI1 = (Onorario1+ccp1+SpeseD1); 
       Deb1 = (Onorario1+ccp1+Iva1+SpeseD1); 
       Netto1 = (Onorario1+ccp1+Iva1+SpeseD1) - Rit1; 
       // 
       ccp.setText(Double.toString(round(ccp1,2))); 
       Iva.setText(Double.toString(round(Iva1,2))); 
       Rit.setText(Double.toString(round(Rit1,2))); 
       CostI.setText(Double.toString(round(CostI1,2))); 
       Deb.setText(Double.toString(round(Deb1,2))); 
       Netto.setText(Double.toString(round(Netto1,2))); 
     } 
}); 
+0

現在,當我按複選框它做calcs,並按鈕導致異常 – user3293120

+0

使用setOnCheckedChangeListener(),wath異常給我更多的細節? – Jorgesys

+0

使用setOnCheckedChangeListener(),它會給出該活動類com.ITE.economiaaziendale中的方法OnClickListener(View)無法找到方法OnLayListener(View) 02-11 19:17:45.071:E/AndroidRuntime(488):java.lang.IllegalStateException: CostiDiImpianto for onClick處理程序在視圖類android.widget.CheckBox ID爲'checkBox2' – user3293120

0

首先全局定義的所有意見,並在你的活動類,即實現你的onCheckedChangedListener:

Edittext Onorario, SpeseD; 
TextView CostI, Iva, Deb, Rit, ccp, Netto; 
CheckBox checkBox1, checkBox2; 
public class YourActivity extends Activity implements onCheckedChangedListener { 

然後做你的onCreate方法,即在初始化過程:

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

Onorario = (EditText) findViewById(R.id.Onorario); 
SpeseD = (EditText) findViewById(R.id.SpeseD); 
CostI = (TextView) findViewById(R.id.CostI); 
Iva = (TextView) findViewById(R.id.Iva); 
Deb = (TextView) findViewById(R.id.Deb); 
Rit = (TextView) findViewById(R.id.Rit); 
ccp = (TextView) findViewById(R.id.ccp); 
Netto = (TextView) findViewById(R.id.Netto); 
checkBox1=(CheckBox)findViewById(R.id.checkBox1); 
checkBox2=(CheckBox)findViewById(R.id.checkBox2); 
//Here add your onCheckedChangedListener to each checkbox 
checkBox1.setOnCheckedChangeListener(this); 
checkBox2.setOnCheckedChangeListener(this); 
} 

現在在你的setOnCheckedChangeListener上做你喜歡的事情即

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
// TODO Auto-generated method stub 

switch (buttonView.getId()){ 

case R.id.checkBox1: 
    if(isChecked){ 
    //Do here all your button click stuff or set text to any edittext or textview if the first ckeckbox is checked.. 

    }else{ 
    //do something if the checkbox1 is not checked 
    } 
    break; 
case R.id.checkBox2: 
    if(isChecked){ 
    //do here what you want to do when checkbox2 is checked 
    }else{ 
    //and here when the checkbox2 is not checked 
    } 
    break; 
} 

這裏所有的答案都是正確的,但你的問題在於寫作標準。 正如我在你的代碼中看到的,你的onCreate方法知道所有東西。因此,以一種好的方式處理所有進程,這將有助於您找到編譯錯誤和運行時錯誤。

相關問題