gpt4 book ai didi

android - 两个编辑文本上的 TextWatcher

转载 作者:行者123 更新时间:2023-11-29 19:03:29 27 4
gpt4 key购买 nike

我有两个编辑文本,我想在编辑另一个文本的同时更改一个文本,反之亦然。我使用了 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) {}
});

但是这个代码不起作用,只能用一个,我需要两个

最佳答案

由于您已在编辑文本和两个回调事件 (onTextChangeListener) 上设置文本更改监听器,因此它将是无限迭代。它将继续在一侧的编辑文本上设置文本,并且回调将继续调用。因此这个循环将一直运行直到应用程序崩溃。编辑要实现您想要的,请参阅下面的详细信息

您需要跟踪当前关注的编辑文本(这是为了在编辑文本中设置文本)。您需要有 2 个 bool 变量。现在看下面的代码

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;
}
});
}

希望对您有所帮助..如果您有任何问题,请随时提问

关于android - 两个编辑文本上的 TextWatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48021816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com