繁体   English   中英

输入EditText时清除TextView

[英]Clear a TextView when EditText inputs

我为数字输入设置了2个EditText字段,按下时在两个输入上启动计算的按钮,以及一个用于显示计算结果的TextView。 对于重复的计算,我想在任一EditText更改后立即清除TextView结果。 在回答“ avalancha”给出的“对EditText字段使用OnClick的更好方法”的答复后,我的程序在第一个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;
} 

使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM