android 监听EditText输入字符长度

jopen 10年前

项目当中的要求很多,最近遇到了一些要求,在Dialog上用户输入密码,当密码位数达到6位并且自动判断密码是否正确,如果正确Dialog自动消失,跳转其他界面。 看似很艰难,其实只要你知道有这样一个监听就简单多了。这个监听就是addTextChangedListener(EditText s).

    public class EditTextActivity extends Activity implements OnClickListener{            private EditText adb_password;            public boolean change = false;            private LinearLayout adb_lin2;            private Button button1;            private Button button2;                        @Override            protected void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.kvadb);                                adb_password = (EditText) findViewById(R.id.adb_password);                adb_lin2 = (LinearLayout) findViewById(R.id.adb_lin2);                button1 = (Button) findViewById(R.id.button1);                button2 = (Button) findViewById(R.id.button2);                                button1.setOnClickListener(this);                button2.setOnClickListener(this);                                adb_password.addTextChangedListener(mEditText);            }                        TextWatcher mEditText = new TextWatcher() {                private CharSequence temp;                                @Override                public void onTextChanged(CharSequence s, int start, int before, int count) {                    temp = s;                }                                @Override                public void beforeTextChanged(CharSequence s, int start, int count,                        int after) {                    adb_password.setVisibility(View.VISIBLE);                    adb_lin2.setVisibility(View.GONE);                }                                @Override                public void afterTextChanged(Editable s) {                    if(adb_password.getText().length() == 6){                        if(adb_password.getText().toString().equals("111111")){                            adb_password.setVisibility(View.GONE);                            adb_password.setFocusableInTouchMode(false);                            adb_password.setFocusable(false);                            adb_lin2.setVisibility(View.VISIBLE);                            adb_lin2.setFocusableInTouchMode(true);                            button1.requestFocus();                        }                    }                        }            };                                @Override            public void onClick(View v) {                switch (v.getId()) {                case R.id.button1:                    Toast.makeText(KvAdb.this, "llll", 0).show();                    break;                case R.id.button2:                            finish();                    break;                }            }        }