editText1_id.Setontextchanged(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editText1_id.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext()," Please insert your User ID", Toast.LENGTH_SHORT).show();
}else if ((editText1_id.length() < 4)){
makeToastLogin("Please insert your User ID");
} else {
userId = editText1_id.getText().toString();
}
if (editText1_id.getText().toString().isEmpty()) {
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() < 4){
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() == 4) {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
} else {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
}
}
});
答案 0 :(得分:1)
您可以将代码放在TextWatcher
内,如下所示:
editText1_id.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 (editText1_id.getText().toString().isEmpty()) {
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() < 4){
editText1_id.setBackgroundResource(R.drawable.buttonshap_red_outline);
}else if (editText1_id.length() == 4) {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
} else {
editText1_id.setBackgroundResource(R.drawable.insert_frame);
}
}
});
如您所见,您可以为每个addTextChangedListener
添加EditText
。它有三个监听器,可以帮助您注意用户何时开始输入
答案 1 :(得分:0)
你不能使用OnClickListener,你需要一个TextWatcher。 请参阅此答案:https://stackoverflow.com/a/11134227/3673616
答案 2 :(得分:0)
听起来就像你正在尝试这样做,除了你想要4而不是2:Android Development: How To Use onKeyUp?