使用复选框在单击按钮时检查数据

时间:2014-03-22 13:04:05

标签: java android math checkbox

我再次,iv尝试按照android复选框示例,虽然它已经工作了大部分,我需要代码只在单击按钮时启动,程序中只有一个按钮,此按钮也是曾经做过数学计算。 我想我需要一个嵌套的if语句,但我只是想知道你们是否有人可以帮我构建这个,当检查checkBox时,我希望它检查文本字段旁边是否有数据复选框,如果有数据,我希望程序显示错误语句,虽然这只是在调用计算按钮后才能完成。 任何人都可以给我任何帮助构建这个,我已经尝试添加监听器,但我已经有一个在calc按钮,如果我在此方法中添加更多,我收到错误消息:/ 有三个复选框,全部用于检查不同的字段,但是我也只想在任何时候检查一个复选框,并且只有在文本字段中没有适用于此的数据。 欢呼的家伙,抱歉,如果我有点模糊,但我开始用我缺乏知识松开我的抹布。 欢呼声

public class Current extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current);
        // Show the Up button in the action bar.
        setupActionBar();

      Button calc1 = (Button)findViewById(R.id.current_calculate);

        calc1.setOnClickListener(new View.OnClickListener() {

              CheckBox checktime = (CheckBox) findViewById(R.id.custom1);
               CheckBox checkcurrent = (CheckBox) findViewById(R.id.custom2);
               CheckBox checkcharge = (CheckBox) findViewById(R.id.custom3);

               public void onClick(View v) {


                        //i would like this part to check that checktime is checked and that there is no data in editText Time1
                if (((CheckBox) v).isChecked()) {



                    Toast.makeText(Current.this,
                       "Bro, try Android :)", Toast.LENGTH_LONG).show();
                }





                        //i would like this part to check that checkCurrent is checked and that there is no data in editText current1
                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(Current.this,
                       "Bro, i mean it ", Toast.LENGTH_LONG).show();
                }



                        //i would like this part to check that checkCharge is checked and that there is no data in editText charge1
                if (((CheckBox) v).isChecked()) {
                    Toast.makeText(Current.this,
                       "Bro, try Android :)", Toast.LENGTH_LONG).show();
                }


            // please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
           // in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
          // any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow

                EditText Charge1 = (EditText)findViewById(R.id.number_input_2);    
                EditText Time1 = (EditText)findViewById(R.id.number_input_3);
                TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
                double charge = Double.parseDouble(Charge1.getText().toString());
                double time = Double.parseDouble(Time1.getText().toString());
    //Time is a class in Java
                Distances_answer.setText("" +charge*time);

        }
    });
    }

2 个答案:

答案 0 :(得分:0)

 public void onClick(View v) {


                        //i would like this part to check that checktime is checked and that there is no data in editText Time1

if(Time1.length()==0){
               checktime.setChecked(true);


                    Toast.makeText(Current.this,
                       "Bro, try Android :)", Toast.LENGTH_LONG).show();
                }





                        //i would like this part to check that checkCurrent is checked and that there is no data in editText current1
              if(current1.length()==0){
               checkCurrent.setChecked(true);



                    Toast.makeText(Current.this,
                       "Bro, i mean it ", Toast.LENGTH_LONG).show();
                }



                        //i would like this part to check that checkCharge is checked and that there is no data in editText charge1
               if(charge1.length()==0){
               checkCharge.setChecked(true);



                    Toast.makeText(Current.this,
                       "Bro, tryAndroid ", Toast.LENGTH_LONG).show();
                }

            // please note that i have not yet added the current.text field yet as i cannot be bothered with it throwing up errors just yet, please bear
           // in mind that i will be doing another two sums on this program, the next sum will be current1 / time1 and then the next will be charge1/current1
          // any help would be much appreciated, for now i only want the program to check that EditText current1 is empty and if so do the calculation bellow

               // EditText Charge1 = (EditText)findViewById(R.id.number_input_2);    
             //   EditText Time1 = (EditText)findViewById(R.id.number_input_3);
               // TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
                double charge = Double.parseDouble(Charge1.getText().toString());
                double time = Double.parseDouble(Time1.getText().toString());
    //Time is a class in Java
                Distances_answer.setText("" +charge*time);

        }
    });
Just make sure that you findviewbyid all the edit texts before the button click.

答案 1 :(得分:0)

首先,因为它是onclicklistener的按钮,v在你的情况下总是按钮。所以((CheckBox) v).isChecked()会给出运行时错误。将其更改为checktime.isChecked()或您要检查其已检查状态的任何其他复选框。至于检查editext的空白,可以通过edittext.getText().toString().isEmpty()

来完成