简体   繁体   English

更改EditText字段的值相互依赖

[英]Values of EditText fields are changed depends on each other

I'm trying to build temperature converter (F -> C and C -> F). 我正在尝试构建温度转换器(F-> C和C-> F)。

I have 2 ET fields. 我有2个ET字段。 when user types in one, the other displays converted value and vice verse. 当用户输入一个时,另一个显示转换后的值,反之亦然。

I understand that similar programs has been build already, but I couldn't find solution. 我了解已经建立了类似的程序,但是找不到解决方案。

It works fine for one field, but app closes when I try to edit the other one. 它适用于一个字段,但是当我尝试编辑另一个字段时,应用程序关闭。

Here is my piece of code: 这是我的代码:

public class Temp extends Activity implements OnClickListener, OnFocusChangeListener {

private EditText temp_f, temp_c;

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.temp);

    temp_f = (EditText) findViewById(R.id.temp_f_inp);
    temp_c = (EditText) findViewById(R.id.temp_c_inp);

    temp_c.setOnFocusChangeListener((OnFocusChangeListener) this);
    temp_f.setOnFocusChangeListener((OnFocusChangeListener) this);

}

private TextWatcher tempc = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (temp_c.getText().length() == 0) 
        {
            temp_f.setText("");
        } else {
            float convValue = Float.parseFloat(temp_c.getText()
                    .toString());

            conv_f = ((convValue - 32) * 5 / 9);

            temp_f.setText(String.valueOf(new DecimalFormat(
                    "##.###").format(conv_f)));
        }

    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    @Override
    public void afterTextChanged(Editable s) {

    }       
};

private TextWatcher tempf = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start,int before, int count) {

        // TODO Auto-generated method stub
                    if (temp_f.getText().length() == 0) 
                    {
                        temp_c.setText("");
                    } else {

                        float convValue = Float.parseFloat(temp_f.getText()
                                .toString());

                        conv_c = ((convValue * 9) / 5 + 32);

                        temp_c.setText(String.valueOf(new DecimalFormat(
                                "##.###").format(conv_c)));

    }
    @Override
    public void beforeTextChanged(CharSequence s, int start,int count, int after) {}
    @Override
    public void afterTextChanged(Editable s) {

    }
};

@Override
public void onFocusChange(View v, boolean hasFocus) {
        if ((v == findViewById(R.id.temp_c_inp)) && (hasFocus==true)) {
            temp_c.addTextChangedListener(tempc);
        }

        else if((v == findViewById(R.id.temp_f_inp)) && (hasFocus==true)){
            temp_f.addTextChangedListener(tempf);
        } 
}

it seems like onTextChanged still holds the values of the first ET that has been modified and when I try to edit the other ET fields, it throws an error. 似乎onTextChanged仍然保存已修改的第一个ET的值,并且当我尝试编辑其他ET字段时,它将引发错误。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thank you! 谢谢!

You could try this: 您可以尝试以下方法:

@Override
 public void onFocusChange(View v, boolean hasFocus) {
     if (v.equals(findViewById(R.id.temp_c_inp))) {
        if(hasFocus){
            temp_c.addTextChangedListener(tempc);
        }else{
            temp_c.removeTextChangedListener(tempc);
        }
     }
    else if(v.equals(findViewById(R.id.temp_f_inp))){
        if(hasFocus){
        I      temp_f.addTextChangedListener(tempf);
        }else{
             temp_f.removeTextChangedListener(tempf);
        }
    } 
}

I haven't tried the code by myself, but I hope it could help you 我没有自己尝试过该代码,但希望它能对您有所帮助

Logic seems to be a problem. 逻辑似乎是一个问题。 What I would do is, 1. On text change, do nothing (or just check for valid input values) 2. On focus change, do conversion and populate other text field. 我将要做的是,1.在更改文本时,不执行任何操作(或仅检查有效的输入值)。2.在更改焦点时,进行转换并填充其他文本字段。 Also, you have some @Override functions which are essentially null functions. 另外,您还有一些@Override函数,它们实际上是空函数。 Why override? 为什么要覆盖?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 牙签绑定彼此依赖的模块 - Toothpick bind modules that depends on each other 我们可以有相互依赖的模型吗? - Can we have models who depends on each other? Java构建相互依赖的事件链 - Java building chain of events which depends on each other 删除 addTextChangedListener() 当 EditText 值以编程方式而不是手动更改时 - Remove addTextChangedListener() When the EditText values changed programmatically not manually Android:让我的EditText字段从其他方法借用变量吗? - Android: Having my EditText fields borrow variables from other methods? 如何在Android Sturdio中使EditText相互关联 - How to make EditText be related to each other in Android Sturdio 如何使两个edittext视图相互依赖 - How to make two edittext views depended to each other 使用Android SharedPreferences或内部存储在本地存储EditText字段的值 - Using Android SharedPreferences or Internal Storage to store values of EditText fields locally 如何通过比较值更改EditText字段的背景颜色? - How do I change background color of EditText fields by comparing values? 如何组合相互依赖的 Observables 并从每个对象中获取一个包含值的对象? - How to combine Observables which depends each other and get an object that contain a value from each?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM