简体   繁体   中英

Enable EditText underline

By Default EditText has an underline. I want to start my program with the Edittext disabled and when someone clicks on an icon, the EditText is enabled.

Here is a picture showing the "to..." EditText not having an underline where the others do and the one with focus has it in the Accent color.

在此处输入图片说明

The code I have tried:

foodIconButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            foodCurrentEditText.setEnabled(true);
            foodDesiredEditText.setEnabled(true);
            foodDesiredEditText.setFocusable(true);
            foodDesiredEditText.setBackgroundColor(Color.TRANSPARENT);
            //foodDesiredEditText.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#03DAC5")));
            //MyDrawableCompat.setColorFilter(foodDesiredEditText.getBackground(), ContextCompat.getColor(MainActivity.this, R.color.colorAccent));

            foodDesiredEditText.getBackground().setColorFilter(ContextCompat.getColor(MainActivity.this, R.color.colorAccent), PorterDuff.Mode.SRC_IN);
        }
    });

The bottom 3 lines of code are the different methods I have tried from suggestions in other questions on here.

If you want to hide and show the underline of the EditText , you can do it like the following:

To hide the line:

ColorStateList colorStateList = ColorStateList.valueOf(Color.TRANSPARENT);
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

To show the line:

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(applicationContext, R.color.colorAccent));
ViewCompat.setBackgroundTintList(foodDesiredEditText, colorStateList);

A better solution if you are using androidx is:

To hide the line:

foodDesiredEditText.setBackgroundResource(0);

To show the line:

foodDesiredEditText.setBackgroundResource(androidx.appcompat.R.drawable.abc_edit_text_material);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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