本文介绍了从textwatcher访问的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是textWatcher:

I am using a textWatcher :

final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView2);

    et.addTextChangedListener(new TextWatcher()
    {
            public void afterTextChanged(Editable s){
                    if(s.length() > 0) {
                    tv.setText(s.toString().substring(s.length()-1));
                    et.setText("");
                }
            }
            public void beforeTextChanged(CharSequence s,int start,int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
    });

这回错误,我无法访问et.setText,因为它最终可能还是因为它锁定键盘,任何想法?

this return error ,I can't access the et.setText because it's final maybe or because it's locked by the Keyboard , any idea ?

登录猫:

06-02 13:35:25.139: D/AndroidRuntime(12532): Shutting down VM
06-02 13:35:25.139: W/dalvikvm(12532): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-02 13:35:25.219: E/AndroidRuntime(12532): FATAL EXCEPTION: main
06-02 13:35:25.219: E/AndroidRuntime(12532): java.lang.StackOverflowError
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView$InsertionPointCursorController.hide(TextView.java:8609)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.hideInsertionPointCursorController(TextView.java:9088)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.hideControllers(TextView.java:9105)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2677)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input$1.beforeTextChanged(Input.java:27)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.sendBeforeTextChanged(TextView.java:6554)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2702)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2662)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.EditText.setText(EditText.java:178)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at android.widget.TextView.setText(TextView.java:2637)
06-02 13:35:25.219: E/AndroidRuntime(12532):    at com.pack.witch.Input

PS:我需要改变什么,例如用户类型,如果他类型A我会告诉D,它使得它看起来像他种的D

Ps : I need to change what the user types for example if he types "A" I'll show "D" it make it looks like he types "D"

推荐答案

试试这样

final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView2);

et.addTextChangedListener(new TextWatcher()
{
        public void afterTextChanged(Editable s){

            }
        }
        public void beforeTextChanged(CharSequence s,int start,int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() > 0) {
                tv.setText(s.toString().substring(s.length()-1));
                et.setText("");}
         }
});

这篇关于从textwatcher访问的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 21:44