我无法更改 EditText1 中的文本以响应 EditText2 中的文本更改

I am unable to change text in EditText1 in response to the text change in EditText2

我希望用户以摄氏度输入温度并以华氏度显示温度,反之亦然。我认为 TextChangedListener 非常适合在不按下任何按钮的情况下进行转换。这些值会随着用户输入而不断变化。

我遇到的最大问题是 EditText 不接受任何输入。在我输入任何内容的那一刻,应用程序强制关闭。请告诉我 wrong.Here 我的代码是什么。

public class MainActivity extends ActionBarActivity {

EditText C, F;
Button exit;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    C = (EditText) findViewById(R.id.celsius);
    F = (EditText) findViewById(R.id.fahrenheit);
    exit = (Button) findViewById(R.id.exit);

    C.addTextChangedListener(new TextWatcher() {
        float f,c;

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            c = Float.parseFloat(s.toString());
            f = (((9 / 5) * c) + 32);
            F.setText((int) f);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    F.addTextChangedListener(new TextWatcher() {
        float f
                ,
                c;

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            f = Float.parseFloat(s.toString());
            c = ((f - 32) * (5 / 9));
            C.setText((int) c);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

}

}

这是我的 Logcat。

01-25 00:55:35.486    7051-7051/com.jainchiranjeev.mylearning7.mylearning_7 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.jainchiranjeev.mylearning7.mylearning_7, PID: 7051
java.lang.WhosebugError
        at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:915)
        at android.text.TextUtils.getChars(TextUtils.java:81)
        at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
        at android.text.TextUtils.getChars(TextUtils.java:81)
        at android.text.TextUtils.indexOf(TextUtils.java:114)
        at android.text.StaticLayout.generate(StaticLayout.java:187)
        at android.text.DynamicLayout.reflow(DynamicLayout.java:288)
        at android.text.DynamicLayout.<init>(DynamicLayout.java:174)
        at android.widget.TextView.makeSingleLayout(TextView.java:6205)
        at android.widget.TextView.makeNewLayout(TextView.java:6103)
        at android.widget.TextView.checkForRelayout(TextView.java:6752)
        at android.widget.TextView.setText(TextView.java:3850)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:66)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:45)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:66)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:45)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:66)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:45)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:66)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(TextView.java:3708)
        at android.widget.EditText.setText(EditText.java:81)
        at android.widget.TextView.setText(TextView.java:3683)
        at com.jainchiranjeev.mylearning7.mylearning_7.MainActivity.onTextChanged(MainActivity.java:45)
        at android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        at android.widget.TextView.setText(TextView.java:3853)
        at android.widget.TextView.setText(

我会实现两个 TextViews 来显示每个计算的结果。您现在正在做的是创建一个无限循环,当在第一个 EditText 中更改文本时触发您的第二个编辑文本 onTextChanged 侦听器,依此类推。因此出现计算器错误。

更改您的实现以在 TextView 中显示每个 EditText 计算,然后问题就会消失。

简而言之,添加您的 TextView

TextView tv = (TextView)findViewById(R.id.textview);

在你的

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    c = Float.parseFloat(s.toString());
    f = (((9 / 5) * c) + 32);
    tv .setText(String.valueOf(f));
}

试试那个代码

我刚刚研究了文档,它说

onTextChanged() 不是修改EditText文本的地方

这里是 link 您可以从那里验证的文档

http://developer.android.com/reference/android/text/TextWatcher.html#onTextChanged%28java.lang.CharSequence,%20int,%20int,%20int%29

所以你可以做的就是将代码移动到文本更改后。

C.addTextChangedListener(new TextWatcher() {
    float f
            ,
            c;

    @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) {
        if (!isAlpha(s.toString)){

            if(!s.toString().equals("")){

                c = Float.parseFloat(s.toString());
                f = (((9 / 5) * c) + 32);
                F.setText(""+ f);
             }
        }
    }
});

这个isAlpha函数是检查用户输入的是数值还是字母值

 public boolean isAlpha(String name) {
    return name.matches("[a-zA-Z]+");
}