gpt4 book ai didi

java - EditText需要改变颜色监听器

转载 作者:行者123 更新时间:2023-12-02 12:44:44 25 4
gpt4 key购买 nike

嘿,这是我的代码,我需要一些帮助,当用户输入他的用户 ID 时,编辑文本需要从红色变为绿色,代码可以正常工作,但只有当您输入您的用户 ID,然后按 Enter 键时,我才需要它自动改变颜色。

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);
}

}

});

最佳答案

您可以将代码放入 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。它有三个监听器,可以帮助您注意到用户何时开始输入

关于java - EditText需要改变颜色监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com