0

我有 3 个复选框和 2 个编辑文本。当用户选中一个复选框并在一个编辑文本中输入数据时,就会发生计算。如果我选中另一个复选框,edittext 值应该会自动清除。然而,即使在我检查了其他复选框之后,edittexts 中的数据仍然存在。我尝试了关闭应用程序的完成()。知道如何在不关闭应用程序的情况下实现这一目标吗?

对不起,如果这个问题听起来很奇怪。我只是学习android 谢谢。

我的 onCheckedChangedListener 的代码

public void onCheckedChanged(CompoundButton predictionChkView, boolean isPredictionChecked)
    {
        // TODO Auto-generated method stub


        switch(predictionChkView.getId())
        {
        case R.id.chkLastMileage1:

                                        isChkLastMileage1=true;
                                        chkLastMileage5.setChecked(false);
                                        chkLastMileage10.setChecked(false);
                                        ETPredictKm.setText("");
                                        ETPredictFuelQty.setText("");

                                        break;

        case R.id.chkLastMileage5:
                                        isChkLastMileage5=true;
                                        chkLastMileage1.setChecked(false);
                                        chkLastMileage10.setChecked(false);
                                        ETPredictKm.setText("");
                                        ETPredictFuelQty.setText("");
                                        break;
        case R.id.chkLastMileage10:

                                        isChkLastMileage10 =true;
                                        chkLastMileage1.setChecked(false);
                                        chkLastMileage5.setChecked(false);
                                        ETPredictKm.setText("");
                                        ETPredictFuelQty.setText("");
                                        break;
        }

    }

Code for the onFocusChangedListener

public void onFocusChange(View predictionFocusView, boolean hasPredictionETFocus) 
    {

        // TODO Auto-generated method stub
        FuelStoredInfo predictInfo = new FuelStoredInfo(this);
        predictInfo.open();
        predictInfo.getAvgMileage(this);
        predictInfo.close();
        try
        {
            predictKm = Long.parseLong(ETPredictKm.getText().toString());
            predictFuetlQty = Double.parseDouble(ETPredictFuelQty.getText().toString());



        }
        catch(NumberFormatException ne)
        {
            ne.printStackTrace();
        }
        if(isChkLastMileage1 ==true || isChkLastMileage5==true||isChkLastMileage10==true)
        {
        if(ETPredictKm.hasFocus())
            {
            ETPredictKm.setText("");
            if(predictFuetlQty!=0)
            {
                predictKm =(long) (predictionMileage*predictFuetlQty);
                //setPredictKm(predictKm);
                ETPredictKm.setText(String.valueOf(predictKm));
            }
            }
            else if(ETPredictFuelQty.hasFocus())
            {
                ETPredictFuelQty.setText("");
                if(predictKm!=0)
                {
                    predictFuetlQty =predictKm/predictionMileage;
                    //setPredictFuetlQty(predictFuetlQty);
                    ETPredictFuelQty.setText(new DecimalFormat("##.##").format(predictFuetlQty));
                }

            }   
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Please check a checkbox!", Toast.LENGTH_LONG).show();
        }

    }

public void onClick(View v) 
    {
        // TODO Auto-generated method stub

                                ETPredictKm.setText("");
                                ETPredictFuelQty.setText("");
                                chkLastMileage1.setChecked(false);
                                chkLastMileage5.setChecked(false);
                                chkLastMileage10.setChecked(false);


    }
4

5 回答 5

0

Add to your function.

EditText editText = (EditText)findViewById(R.id.edittext);
editText.setText("");
于 2013-07-21T13:56:54.150 回答
0

When you check the other checkbox do this

 edittext.setText("");

for other EditText

Edit -----

Add FocusChangeListener for all of your EditText and do this ----

ETPredictKm.setOnFocusChangeListener(new OnFocusChangeListener()
    {
        @Override
        public void onFocusChange(View v, boolean hasFocus) 
        {
            if (hasFocus)
                ETPredictKm.setText("");
        }
    });
于 2013-07-21T13:57:19.000 回答
0

If you store a reference to your editText you can just call

editText.setText("");

Do this inside your onCheckedChange listener when the checkbox is in your desired state.

于 2013-07-21T13:58:44.837 回答
0
CheckBox check1 = (CheckBox) findViewById(R.id.checkbox1);
Checkbox check2 = (Checkbox) findViewById(R.id.checkbox2);
 if (check1.isChecked()) {
     EditText myEditText = (EditText) findViewById(R.id.edit_text_1);
     myEditText.setText("");
 }
 if (check2.isChecked()) {
     EditText myEditText2 = (EditText) findViewById(R.id.edit_text_2);
     myEditText1.setText("");
 }
于 2013-07-21T16:47:44.660 回答
0

添加一个复选框 oncheckedchangelistener 然后把 edittest.setText(""); 里面。

做这样的事情:

checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        edittext.setText("");
        }
    });

另外,不要忘记为您的编辑文本、复选框等声明变量,如下所示:

//example
final CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);

让我知道它是否有效:)

选择:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
        {
            if ( checkbox.isChecked() )
            {
                edittext.setText("");
            }

        }
    });
于 2013-07-21T14:03:49.240 回答