EditText中的问题

时间:2016-04-27 11:14:41

标签: java android

我的问题是,如果第一个EditText长度大于10,那么第二个EditText将被启用,第二个EditText长度大于8,那么在Button之后将启用我的代码中的问题?请任何人都可以解决这个问题。

public void loginPage(View v) {
    editText = (EditText) findViewById(R.id.username);
    editText1 = (EditText) findViewById(R.id.pasword);
    username = editText.getText().toString();
    password = editText1.getText().toString();
    btn = (Button) findViewById(R.id.login1);
    editText1.setEnabled(false);
    btn.setEnabled(false);
    editText.addTextChangedListener(new TextWatcher() {
        @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 (s.length() >= 10) {
                editText1.setEnabled(true);
                btn.setEnabled(false);
            }
        }
    });
    editText1.addTextChangedListener(new TextWatcher() {
        @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 (s.length() >= 8) {
                btn.setEnabled(true);
            }
        }
    });
}

2 个答案:

答案 0 :(得分:5)

将此用于等于或大于10个单词

将此方法粘贴到OnCreate()方法

editText.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 (s.length() >= 10)
                editText1.setEnabled(true);
            else
                editText1.setEnabled(false);
        }
    });

和8个字的按钮

将此方法粘贴到OnCreate()方法

editText1.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 (s.length() >= 8)
                    btn.setEnabled(true);
               else
                    btn.setEnabled(false);
            }
        });

享受编码.........

答案 1 :(得分:2)

您需要将Text WatcherEdit Text一起使用。

Try this code.

public class Login extends AppCompatActivity {

    EditText editText, editText1;
    Button btn;
    String username, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        loginPage();
    }

    public void loginPage()

    {

        editText = (EditText) findViewById(R.id.username);
        editText1 = (EditText) findViewById(R.id.password);
        username = editText.getText().toString();
        password = editText1.getText().toString();
        btn = (Button) findViewById(R.id.login1);
        editText1.setEnabled(false);
        btn.setEnabled(false);
        editText.addTextChangedListener(new TextWatcher() {
            @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 (s.length() >= 10) {
                    editText1.setEnabled(true);
                    btn.setEnabled(false);
                }
            }
        });
        editText1.addTextChangedListener(new TextWatcher() {
            @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 (s.length() >= 8) {
                    btn.setEnabled(true);
                }
            }
        });
    }
}

编辑:

Create new function to Change from Login page on Button Click.

public void changeLoginPage(){
     Intent intentNew = new Intent(First.this,Destinatin.this);
     startActivity(intentNew);
}

并在onCreate方法上添加此内容。

btn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        changeLoginPage();
     }
});