在 android 上同时编辑两个文本
Two simultanious edit texts on android
我正在为 android 创建一个单位转换器。它有两个编辑文本,我想在用户输入数字时更新一个,但我无法获取它。
我已经尝试了几种方法,但到目前为止我无法使它起作用。我是 android 的新手,我已经在 java 中制作了这个应用程序,但在这里我似乎无法理解。
到目前为止,我已经掌握了使用点击方法的方法,但我非常希望它能在用户在编辑文本中输入数字时显示转换结果
用 addTextChangedListener
这样做:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do your conversion
yourSecondEditText.setText(text);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
您应该按照 Chadi 的建议添加一个 TextChangedListener,但我建议以不同的模式实施。看看下面TextWatcher
的实现
EditText firstEdit = (EditText) findViewById(R.id.firstEdit);
firstEdit.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) {
//Do here your conversion
secondEdit.setText(convertedText);
}
});
我建议您使用 afterTextChanged,因为正如官方文档所述,您可以确定文本实际上已经更改。
官方文档:
This method is called to notify you that, somewhere within s, the text
has been changed. It is legitimate to make further changes to s from
this callback, but be careful not to get yourself into an infinite
loop, because any changes you make will cause this method to be called
again recursively. (You are not told where the change took place
because other afterTextChanged() methods may already have made other
changes and invalidated the offsets. But if you need to know here, you
can use setSpan(Object, int, int, int) in onTextChanged(CharSequence,
int, int, int) to mark your place and then look up from here where the
span ended up.
我正在为 android 创建一个单位转换器。它有两个编辑文本,我想在用户输入数字时更新一个,但我无法获取它。
我已经尝试了几种方法,但到目前为止我无法使它起作用。我是 android 的新手,我已经在 java 中制作了这个应用程序,但在这里我似乎无法理解。
到目前为止,我已经掌握了使用点击方法的方法,但我非常希望它能在用户在编辑文本中输入数字时显示转换结果
用 addTextChangedListener
这样做:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//do your conversion
yourSecondEditText.setText(text);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
您应该按照 Chadi 的建议添加一个 TextChangedListener,但我建议以不同的模式实施。看看下面TextWatcher
EditText firstEdit = (EditText) findViewById(R.id.firstEdit);
firstEdit.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) {
//Do here your conversion
secondEdit.setText(convertedText);
}
});
我建议您使用 afterTextChanged,因为正如官方文档所述,您可以确定文本实际上已经更改。
官方文档:
This method is called to notify you that, somewhere within s, the text has been changed. It is legitimate to make further changes to s from this callback, but be careful not to get yourself into an infinite loop, because any changes you make will cause this method to be called again recursively. (You are not told where the change took place because other afterTextChanged() methods may already have made other changes and invalidated the offsets. But if you need to know here, you can use setSpan(Object, int, int, int) in onTextChanged(CharSequence, int, int, int) to mark your place and then look up from here where the span ended up.