在EditText输入时清除TextView

时间:2015-03-16 18:14:01

标签: java android android-edittext

我有2个EditText字段设置为数字输入,按钮在按下时开始计算2个输入,TextView显示计算结果。对于重复计算,我想在EditText更改后立即清除TextView结果。 回复" OnClick for EditText字段的更好方法"由#avanacha'给出,我的程序在第一个EditText字段更改时清除结果,但如果只更改了第二个EditText字段,则保留前一个答案。然而,我在两个领域都使用了相同的源代码。 有人可以解释为什么,以及如何治愈这个?我的代码附后:

public class DoublesActivity extends ActionBarActivity {

private EditText textBox1, textBox2;
private Button calcButton;
private Context context;


@Override
protected void onCreate(Bundle outState) {
    super.onCreate(outState);
    setContentView(R.layout.activity_doubles);    // Sets the layout .xml   file

    context = this.getApplicationContext();

    textBox1 = (EditText) findViewById(R.id.editText1);  //textBox1 holds a reference to the editText1 object in the xml layout
    textBox2 = (EditText) findViewById(R.id.editText2);
    textBox1.setText("");
    textBox2.setText("");

    final TextView textBox3 = (TextView) findViewById(R.id.textView1);

    textBox2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v2, boolean hasFocus2) {
            if (hasFocus2) {
                textBox3.setText("");
            }
        }
    });

    textBox1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v1, boolean hasFocus1) {
            if (hasFocus1) {
                textBox3.setText("");
            }
        }
    });


    calcButton = (Button) findViewById(R.id.button1);
    calcButton.setBackgroundColor(Color.CYAN);
    calcButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            CharSequence userNumber1 = textBox1.getText();                    //userNumber1 is a CharSequence holding the text in textBox1
            CharSequence userNumber2 = textBox2.getText();
            Float handicap1 = Float.parseFloat(userNumber1.toString());     //convert to integer
            Float handicap2 = Float.parseFloat(userNumber2.toString());     //convert to integer
            Float handicapT = calculate(handicap1, handicap2);
            CharSequence userNumber = String.valueOf(handicapT);
            if (handicapT > 98.5) {
                userNumber = "Non-valid h'cap 1!";
            }
            if (handicapT < -98.5) {
                userNumber = "Non-valid h'cap 2!";
            }

            textBox3.setText(userNumber);                    // put result in the TextView
        }
    });
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    TextView textBox3 = (TextView) findViewById(R.id.textView1);
    CharSequence userNumber = textBox3.getText();
    outState.putCharSequence("savedText", userNumber);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    final TextView textBox3 = (TextView) findViewById(R.id.textView1);
    CharSequence userText = savedInstanceState.getCharSequence("savedText");
    textBox3.setText(userText);
}

Float calculate(Float h1, Float h2) {
    float[] handicapArray;
    handicapArray = new float[29];
    handicapArray[0] = 28;
    handicapArray[1] = 26;
    handicapArray[2] = 24;
    handicapArray[3] = 22;
    handicapArray[4] = 20;
    handicapArray[5] = 18;
    handicapArray[6] = 16;
    handicapArray[7] = 14;
    handicapArray[8] = 12;
    handicapArray[9] = 11;
    handicapArray[10] = 10;
    handicapArray[11] = 9;
    handicapArray[12] = 8;
    handicapArray[13] = 7;
    handicapArray[14] = 6;
    handicapArray[15] = 5;
    handicapArray[16] = 4.5F;
    handicapArray[17] = 4;
    handicapArray[18] = 3.5F;
    handicapArray[19] = 3;
    handicapArray[20] = 2.5F;
    handicapArray[21] = 2;
    handicapArray[22] = 1.5F;
    handicapArray[23] = 1;
    handicapArray[24] = 0.5F;
    handicapArray[25] = 0;
    handicapArray[26] = -0.5F;
    handicapArray[27] = -1;
    handicapArray[28] = -1.5F;
    int index1 = -1;
    for (int i = 0; i < 29; i++) {
        if (Math.abs(h1 - handicapArray[i]) < 0.001) {
            index1 = i;
            break;
        }
    }
    if (index1 == -1) {
        EditText textBox1 = (EditText) findViewById(R.id.editText1);
        textBox1.setText("");
    }

    int index2 = -1;
    for (int i = 0; i < 29; i++) {
        if (Math.abs(h2 - handicapArray[i]) < 0.001) {
            index2 = i;
            break;
        }
    }
    if (index2 == -1) {
        EditText textBox2 = (EditText) findViewById(R.id.editText2);
        textBox2.setText("");
    }
    int indexT = (index1 + index2) / 2;   // Correctly rounds indexT halves down.
    Float result = handicapArray[indexT];
    if (index1 == -1) {
        result = 99F;
    }
    ;
    if (index2 == -1) {
        result = -99F;
    }
    ;
    return result;
} 

1 个答案:

答案 0 :(得分:1)

使用addTextChangedListener清除textview。

editText.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      resultTextView.setText("");
   }
  });

例如,请使用以下链接

android on Text Change Listener