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

时间:2015-01-24 19:10:54

标签: android android-edittext converter forceclose textchanged

我希望用户以摄氏度输入温度并以华氏温度显示温度,反之亦然。我认为TextChangedListener无需按任何按钮即可完美转换。当用户键入时,值会继续变化。

我遇到的最大问题是EditText不会接受任何输入。在我输入任何内容的那一刻,app力就会关闭。请告诉我出了什么问题。这是我的代码。

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.StackOverflowError
        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$2.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$1.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$2.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$1.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$2.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$1.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$2.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$1.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(

2 个答案:

答案 0 :(得分:0)

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

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

简而言之,请添加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));
}

答案 1 :(得分:0)

尝试该代码

我刚刚研究了文档,并说它

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

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

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

这个alpha函数用于检查用户是否输入了数值或alphabatic值

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