我有两个edittexts,当我在另一个上发短信时我想要改变一个,反之亦然。我使用了TextWatcher,它或多或少都是这样的:
//MUDAR AO DIGITAR
edt1.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//quando o texto é alterado chamamos o filtro.
double valor = (s.length()>0)?Double.parseDouble(s.toString()):0;
valor = (valor * 100);
edt2.setText(valor);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
edt2.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
double valor = (s.length()>0)?Double.parseDouble(s.toString()):0;
valor = (valor / 100);
edt1.setText(valor);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
但是这段代码不起作用,只适用于一个,我需要两个
答案 0 :(得分:2)
由于您已在两个回调事件(onTextChangeListener)上的编辑文本和设置文本上设置了textChangeListener,因此它将是无限次迭代。它将继续在一侧的edittext上设置文本,并且回调将继续调用。因此,这个循环将继续运行,直到应用程序崩溃。 的修改 要实现您想要的目标,请参阅以下详细信息
您需要定期跟踪您所关注的编辑文本(这是用于在编辑文本中设置文本)。你需要有2个布尔变量。现在看下面的代码
public class MainActivity extends AppCompatActivity {
EditText edt1, edt2;
boolean et1Focus, et2Focus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1 = findViewById(R.id.et1);
edt2 = findViewById(R.id.et2);
edt1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//quando o texto é alterado chamamos o filtro.
if (et1Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor * 100);
edt2.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (et2Focus) {
double valor = (s.length() > 0) ? Double.parseDouble(s.toString()) : 0;
valor = (valor / 100);
edt1.setText(String.valueOf(valor));
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et1Focus = b;
}
});
edt2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
et2Focus = b;
}
});
}
}
希望有所帮助..如果您有任何问题可以随意提问
答案 1 :(得分:0)
//在OnCreate方法中尝试使用此代码调用initViews。
EditText edt1, edt2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
edt1.requestFocus();
edt1.addTextChangedListener(this);
edt1.addTextChangedListener(this);
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
{
}
@Override
public void afterTextChanged(Editable editable)
{
if (editable == edt1.getEditableText())
{
}
else if (editable == edt2.getEditableText())
{
}
}
答案 2 :(得分:0)
你做得差不多但你必须使用afterTextChanged而不是onTextChanged方法
edt1.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {
//quando o texto é alterado chamamos o filtro.
double valor = (s.length()>0)?
Double.parseDouble(s.toString()):0;
valor = (valor * 100);
edt2.setText(valor);}
});
edt2.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void afterTextChanged(Editable s) {
double valor = (s.length()>0)?Double.parseDouble(s.toString()):0;
valor = (valor / 100);
edt1.setText(valor);}
});
答案 3 :(得分:0)
在尝试创建货币转换器应用时,我遇到了同样的问题。 (editText 1中的更改值应更改editText 2中的值,反之亦然)。
我解决这个问题的方法是创建两个文本观察者。
private TextWatcher TextWatcherClass = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
editText2.removeTextChangedListener(TextWatcherClass2);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
editText2.addTextChangedListener(TextWatcherClass2);
}
};
private TextWatcher TextWatcherClass2 = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
editText1.removeTextChangedListener(TextWatcherClass);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
editText1.addTextChangedListener(TextWatcherClass);
}
};
希望这有帮助。