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

戏子无情 提交于 2019-12-11 09:39:34

问题


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(

回答1:


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



回答2:


try that code

i have just studied the documentation and it says that

onTextChanged() is not a place to modify text of EditText

here is the link for documentation you can verify from there

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

so wahat you can do is to move the code to after text change.

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

this isAlpha function is to check whether user entered numeric value or alphabatic value

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


来源:https://stackoverflow.com/questions/28129234/i-am-unable-to-change-text-in-edittext1-in-response-to-the-text-change-in-editte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!