Anj*_*tel 5 android button android-toast
我想在单击禁用按钮时显示 Toast 消息。
button.setEnable(false);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v)
{
Toast.makeText(SliderDemo.this, "Button Disabled",Toast.LENGTH_LONG).show(); }}
Run Code Online (Sandbox Code Playgroud)
我们可以在同一个按钮上同时使用触摸监听器和点击监听器吗?
您无法单击禁用的按钮。尝试这样做,
// if you want to show it as disabled simply change the button background and text color
button.setActivated(false);
button.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.disabled_background_color));
button.setTextColor(ContextCompat.getColor(getContext(),R.color.disabled_text_color));
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View v){
if(!button.isActivated()){
Toast.makeText(SliderDemo.this, "Button Disabled",Toast.LENGTH_LONG).show();
return;
}
//else do your stuff
}
Run Code Online (Sandbox Code Playgroud)
在您的 color.xml 中添加此行
<color name="disabled_background_color">#10181818</color>
<color name="disabled_text_color">#aaa</color>
Run Code Online (Sandbox Code Playgroud)