如何使用addTextChangedListener和内存处理 [英] how to handle with addTextChangedListener and memory
本文介绍了如何使用addTextChangedListener和内存处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class MainActivity extends AppCompatActivity {
EditText Percent, mmolGlic, mgGlic;
double mmol = 0, mg = 0, perc = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Percent = (EditText) findViewById(R.id.percent);
mmolGlic = (EditText) findViewById(R.id.mmol_glic);
mgGlic = (EditText) findViewById(R.id.mg_glic);
/*mmolGlic.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) {
frommMol();
}
@Override
public void afterTextChanged(Editable s) {
}
});*/
Percent.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) {
fromPercent();
}
@Override
public void afterTextChanged(Editable s) {
}
});
/*mgGlic.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) {
}
@Override
public void afterTextChanged(Editable s) {
frommg();
}
});*/
}
public void frommMol() {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol/10.929) + 2.15;
Percent.setText(String.format( "%.2f", perc ));
}
public void fromPercent(){
perc = Double.parseDouble(Percent.getText().toString());
mmol = (perc - 2.15) * 10.929;
mmolGlic.setText(String.format( "%.2f", mmol ));
mg = (perc*28.7) - 46.7;
mgGlic.setText(String.format( "%.2f", mg ));
}
public void frommg(){
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.setText(String.format( "%.2f", perc ));
}
}
大家早安:)
this is a continuous question from this one: Question 1
这是我正在尝试执行的示例代码.但是我有一些问题.我认为其中大多数是针对变量的逻辑以及如何处理更多EditTexts中的输入的.例如:
this an example code of what i'm trying to do. But i have some problems. I think most of them are for the logic of variables and how to handle the input in more EditTexts. for example:
- 主要问题是我不能使用多个addTextChangedListener.我尝试更好地解释一下:如果我将代码保留在此处,则应用程序将崩溃.我不确定,也许是由于我如何处理三个EditText.
- 然后,我删除文本时遇到问题:如果我有"5.99"并且我 按del键,删除时会删除直到5,然后再按 崩溃.可能我应该在文本字段为空时设置 变量= 0.
- the main problem is that i can't use more than one addTextChangedListener. I try to explain better: if i leave the code as here, the app crashes. I am not sure, maybe it's due to how i handle the three EditTexts.
- then i have a problem when i delete the text: if i have "5.99" and i press del, when it's deleting it deletes until the 5 and then crashes. Probaly i should set when the text field is empty the variables = 0.
你能帮我吗?非常感谢你
can you help me? thank you very much
推荐答案
public class MainActivity extends AppCompatActivity {
EditText Percent, mmolGlic, mgGlic;
double mmol = 0, mg = 0, perc = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Percent = (EditText) findViewById(R.id.percent);
mmolGlic = (EditText) findViewById(R.id.mmol_glic);
mgGlic = (EditText) findViewById(R.id.mg_glic);
Percent.addTextChangedListener(percentWatcher);
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
public void frommMol() {
if (!mmolGlic.getText().toString().trim().isEmpty()) {
mmol = Double.parseDouble(mmolGlic.getText().toString());
perc = (mmol / 10.929) + 2.15;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
public void fromPercent() {
if (!Percent.getText().toString().trim().isEmpty()) {
perc = Double.parseDouble(Percent.getText().toString().trim());
mmol = (perc - 2.15) * 10.929;
mmolGlic.removeTextChangedListener(mmolGlicTextWatcher);
mgGlic.removeTextChangedListener(mgGlicWatcher);
mmolGlic.setText(String.format("%.2f", mmol));
mg = (perc * 28.7) - 46.7;
mgGlic.setText(String.format("%.2f", mg));
mmolGlic.addTextChangedListener(mmolGlicTextWatcher);
mgGlic.addTextChangedListener(mgGlicWatcher);
}
}
public void frommg() {
if (!mgGlic.getText().toString().trim().isEmpty()) {
mg = Double.parseDouble(mgGlic.getText().toString());
perc = (mg + 46.7) / 28.7;
Percent.removeTextChangedListener(percentWatcher);
Percent.setText(String.format("%.2f", perc));
Percent.addTextChangedListener(percentWatcher);
}
}
private TextWatcher percentWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
fromPercent();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mgGlicWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommg();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private TextWatcher mmolGlicTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
frommMol();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
}
Hope this will help you.
这篇关于如何使用addTextChangedListener和内存处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文