Can I make a button appear disabled and still listen for clicks?

前端 未结 6 998
栀梦
栀梦 2021-01-11 11:32

I\'d like to gray out a button so it appears disabled to the user, but still listen for clicks so that I can display a message to the user that explains why the button isn\'

6条回答
  •  迷失自我
    2021-01-11 11:35

    If your button is on the newer types that controls color via backgroundTint rather then background, this is what you should do:

    if (enabled) {
        ViewCompat.setBackgroundTintList(btn, getResources().getColorStateList(R.color.button_states)); // either use a single color, or a state_list color resource
    } else {
        ViewCompat.setBackgroundTintList(btn, ColorStateList.valueOf(Color.GRAY));
    }
    // make sure we're always clickable
    btn.setEnabled(true);
    btn.setClickable(true);
    

提交回复
热议问题