输入文本时的Android EditText

时间:2014-04-23 14:44:04

标签: android android-edittext listener

我是android的新手,我不知道当用户在edittext中输入某些内容时,它是否可能会分解另一种方法。

像       EDİT当我在onCrate中运行它时崩溃

     protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment()).commit();
    }
    anyStringEntered();
}

我的方法检查Edittext是否有任何更改

public void anyStringEntered(){
    userName  = (EditText) findViewById(R.id.userNameTextBox);
    userPassword = (EditText) findViewById(R.id.userPasswordTextBox);
    rememberMe= (CheckBox) findViewById(R.id.checkBox1);
    userName.addTextChangedListener(new TextWatcher() {

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


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

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!userPassword.getText().toString().equals("") && !userName.getText().toString().equals("")){
                rememberMe.setEnabled(true);
            }else{
                rememberMe.setEnabled(false);
            }

        }
    });
    userPassword.addTextChangedListener(new TextWatcher() {

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


        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(!userPassword.getText().toString().equals("") && !userName.getText().toString().equals("")){
                rememberMe.setEnabled(true);
            }else{
                rememberMe.setEnabled(false);
            }
        }
    });
}

2 个答案:

答案 0 :(得分:2)

使用TextWatcher

edittext.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        //method1()  
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){
        //method2()
    }
    public void onTextChanged(CharSequence s, int start, int before, int count){
        //method3()
    }
}); 

答案 1 :(得分:0)

这是你在找什么?

editTextLogin.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(edittext.gettext()!="")
                            { myMethod(); } 
                         else {
                              myMethod2();
                           }

        }
    };