我尝试做一个小界面,在其中输入一个数字,计算结果显示在文本字段中。我在同一个界面中进行了几次操作。所以我尝试了一个常规的 addtextchangedlistener ,它可以工作,但我注意到我需要填充我添加了监听器的每个 editText 才能工作。
这是我的第一个代码:
package be.thebeps.easyconverter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.text.*;
public class MainActivity extends Activity implements TextWatcher {
private Double calcula;
private EditText calcula1;
private TextView calcula2;
private Double calculb;
private EditText calculb1;
private TextView calculb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcula1 = (EditText) findViewById(R.id.calcula1);
calcula1.addTextChangedListener(this);
calcula2 = (TextView) findViewById(R.id.calcula2);
calculb1 = (EditText) findViewById(R.id.calculb1);
calculb1.addTextChangedListener(this));
calculb2 = (TextView) findViewById(R.id.calculb2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void afterTextChanged(Editable s) {
try {
calcula = Double.parseDouble(calcula1.getText().toString());
calcula = calcula*0.621;
calcula2.setText(String.format("%.3f", calcula));
calculb = Double.parseDouble(calculb1.getText().toString());
calculb = calculb*2.204;
calculb2.setText(String.format("%.2f", calculb));
} catch(NumberFormatException nfe) {
calcula2.setText(Integer.toString(0));
calculb2.setText(Integer.toString(0));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
}
然后由于它没有按预期工作,我尝试实现一个类来实例化每个editText字段上的textWatcher,所以这是我的实际代码:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.text.*;
public class MainActivity extends Activity {
private Double calcula;
private EditText calcula1;
private TextView calcula2;
private Double calculb;
private EditText calculb1;
private TextView calculb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calcula1 = (EditText) findViewById(R.id.calcula1);
calcula1.addTextChangedListener(new CustomTextWatcher(calcula1));
calcula2 = (TextView) findViewById(R.id.calcula2);
calculb1 = (EditText) findViewById(R.id.calculb1);
calculb1.addTextChangedListener(new CustomTextWatcher(calculb1));
calculb2 = (TextView) findViewById(R.id.calculb2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText e) {
mEditText = e;
}
@Override
public void afterTextChanged(Editable s) {
try {
calcula = Double.parseDouble(calcula1.getText().toString());
calcula = calcula*1.451;
calcula2.setText(String.format("%.3f", calcula));
calculb = Double.parseDouble(calculb1.getText().toString());
calculb = calculb*3.206;
calculb2.setText(String.format("%.2f", calculb));
} catch(NumberFormatException nfe) {
calcula2.setText(Integer.toString(0));
calculb2.setText(Integer.toString(0));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {
}
}
}
但是,如果仅填充了 2 个字段中的一个,则当我更改它时它不会更新(我的意思是不调用 aftertextchanged 方法)。我想在创建视图时已经填充了editText,但是,如果有人进入一个editText(例如calcula1)并清除它(例如用退格键)然后进入calculb1,他可以输入他想要的每个数字, calculb2 不会被修改。我知道这很棘手,而且我有点挑剔,但如果有人能帮助我,我会很高兴。