繁体   English   中英

EditText需要更改颜色侦听器

[英]EditText needs to change color listener

嘿,这是我的代码,我需要一些帮助,当用户输入用户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 它具有三个侦听器,可帮助您在用户开始键入内容时注意到它们

您不能使用OnClickListener,需要一个TextWatcher。 看到这个答案: https : //stackoverflow.com/a/11134227/3673616

听起来好像您正在尝试执行此操作,但您想要的不是2,而是4: Android开发:如何使用onKeyUp?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM