Android EditText TextChanged

时间:2016-03-04 04:39:36

标签: android android-edittext

我想在edittext change(edittext length = 6)时启动新活动。我使用下面的代码,但没有工作。请帮帮我。

    a = (EditText) findViewById(R.id.editText1);
    a.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            if(!s.equals("6") )
                    i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);
                    finish();                
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

        }
       });

4 个答案:

答案 0 :(得分:2)

我担心你是如何检查长度的,只需在afterTextChanged

中替换你的代码
 @Override
        public void afterTextChanged(Editable s) {
            if(a.getText().length()== 6 ) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
                finish();
            }
        }

答案 1 :(得分:0)

试试这个

a.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                        if(count==6 ){
                        i = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(i);
                        finish();  
            }
            }
           });

答案 2 :(得分:0)

试试这段代码

a.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {

                if(s.length() == 6)
                {
                    Intent in = new Intent(CurrentActivity.this,TargetActivity.class);
                    startActivity(in);
                }

            }
        });

如果这适合您,请告诉我! :)

答案 3 :(得分:0)

更新

 if(!s.equals("6") )

to 

 if(s.length()==6 )