我创建了一个转换器应用程序,我使用了2个编辑文本,其中一个获取输入和其他显示输出,它将通过单击按钮显示输出是否有任何方法可以在不使用按钮的单击事件的情况下将输出显示在2个编辑文本中。当我们在1个编辑文本中输入输入时,我想将结果自动放入2个编辑文本中。
EditText input,result;
input = (EditText) findViewById(R.id.ip);
result = (EditText) findViewById(R.id.res); // my edit texts input and result
result.setClickable(false);
public void convert(View view){ //when clicking it get result to 2 edit text, but I want to get automatically to the second edit text when user enter the input
if (!input.getText().toString().equals("")){
ufrom = (String) sp1.getSelectedItem();
uto = (String) sp2.getSelectedItem();
Double ip = Double.valueOf(input.getText().toString());
TemperatureConverter.Units fromUnit = TemperatureConverter.Units.fromString(ufrom);
TemperatureConverter.Units toUnit = TemperatureConverter.Units.fromString(uto);
double r = con.TemperatureConvert(fromUnit,toUnit,ip);
result.setText(String.valueOf(r));
} else {
result.setText("");
}
}
答案 0 :(得分:2)
您可以将TextWatcher用于此目的。您将获得此侦听器中的每个字符值。根据您的需要更新输出editText
editText1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// Place the logic here for your output edittext
}
});
答案 1 :(得分:0)
是的,您可以通过检测键盘操作完成
即。
et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// set result in second edit text
return true;
}
return false;
}
});
答案 2 :(得分:0)
在这里你可以为这样的输入EditText添加TextWatcher,
input.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Check some Validations and Conditions here
// Convert function goes here if all validationand conditions checked
convert();
}
@Override
public void afterTextChanged(Editable s) {
}
});
希望它会帮助你我的朋友!