无法响应于EditText2文本变化而改变在EditText1文

无法响应于EditText2文本变化而改变在EditText1文

本文介绍了我无法响应于EditText2文本变化而改变在EditText1文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户在华氏,反之亦然进入摄氏温度并显示温度。我认为TextChangedListener会是完美的,没有任何按钮pressing转换。值去改变在用户类型。

我遇到的最大的问题是,EditText上不会采取任何输入。那一刻我输入什么,该应用程序强制关闭。请告诉我什么是wrong.Here是我的code。

 公共类MainActivity扩展ActionBarActivity {的EditText C,F;
按钮退出;@覆盖
保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    C =(EditText上)findViewById(R.id.celsius);
    F =(EditText上)findViewById(R.id.fahrenheit);
    出口=(按钮)findViewById(R.id.exit);    C.addTextChangedListener(新TextWatcher(){
        浮动F,C;        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            C = Float.parseFloat(s.toString());
            F =(((9/5)* C)+ 32);
            F.setText((INT)F);
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    });    F.addTextChangedListener(新TextWatcher(){
        浮动˚F
                ,
                C;        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            F = Float.parseFloat(s.toString());
            C =((F - 32)*(5/9));
            C.setText((INT)C);
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    });}

}

下面是我的LogCat中。

  01-25 00:55:35.486 7051-7051 / com.jainchiranjeev.mylearning7.mylearning_7 E / AndroidRuntime:致命异常:主要
工艺:com.jainchiranjeev.mylearning7.mylearning_7,PID:7051
java.lang.StackOverflowError
        在android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:915)
        在android.text.TextUtils.getChars(TextUtils.java:81)
        在android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
        在android.text.TextUtils.getChars(TextUtils.java:81)
        在android.text.TextUtils.indexOf(TextUtils.java:114)
        在android.text.StaticLayout.generate(StaticLayout.java:187)
        在android.text.DynamicLayout.reflow(DynamicLayout.java:288)
        在android.text.DynamicLayout<&初始化GT;(DynamicLayout.java:174)
        在android.widget.TextView.makeSingleLayout(TextView.java:6205)
        在android.widget.TextView.makeNewLayout(TextView.java:6103)
        在android.widget.TextView.checkForRelayout(TextView.java:6752)
        在android.widget.TextView.setText(TextView.java:3850)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 2.onTextChanged(MainActivity.java:66)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 1.onTextChanged(MainActivity.java:45)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 2.onTextChanged(MainActivity.java:66)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 1.onTextChanged(MainActivity.java:45)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 2.onTextChanged(MainActivity.java:66)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 1.onTextChanged(MainActivity.java:45)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 2.onTextChanged(MainActivity.java:66)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(TextView.java:3708)
        在android.widget.EditText.setText(EditText.java:81)
        在android.widget.TextView.setText(TextView.java:3683)
        在com.jainchiranjeev.mylearning7.mylearning_7.MainActivity $ 1.onTextChanged(MainActivity.java:45)
        在android.widget.TextView.sendOnTextChanged(TextView.java:7610)
        在android.widget.TextView.setText(TextView.java:3853)
        在android.widget.TextView.setText(


解决方案

我想实现两个 TextViews 来显示每个计算的结果。你在做什么,现在是创造一个无限循环,在改变文本时的第一个的EditText 触发第二个编辑的文本 onTextChanged 监听器,等等。因此,计算器错误。

更改您的实现,以显示每个的EditText 计算一个的TextView ,那么问题将不复存在。

总之,添加的TextView

  TextView的电视=(的TextView)findViewById(R.id.textview);

和在

  @覆盖
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    C = Float.parseFloat(s.toString());
    F =(((9/5)* C)+ 32);
    电视.setText(将String.valueOf(F));
}

I want the user to enter a temperature in Celsius and display the temperature in fahrenheit and vice versa. I think TextChangedListener would be perfect to convert without any button pressing. The values go on changing as the user types in.

The biggest problem I'm having is that the EditText won't take any input. The moment I input anything, the app force closes. Please tell me what's wrong.Here's my code.

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) {

        }
    });

}

}

Here's my 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(
解决方案

I would implement two TextViews to display the result of each calculation. What you are doing now is creating an infinite loop, when changing text in the first EditText trigger your second edit texts onTextChanged listener, and so on. Hence the stackoverflow error.

Change your implementation to display each EditText calculation in a TextView, then the problem will be gone.

In short, add your TextView

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

and in your

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

这篇关于我无法响应于EditText2文本变化而改变在EditText1文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 12:50