简体   繁体   中英

How to toast a message when you click on a disabled button?

I have some disabled* buttons in an android app. How can I toast a message when you try to click on them ?

what about showin an enabled button as disabled ?

first you have to create two backgrounds disabled.xml and enabled.xml here is a website that helps you to do this http://angrytools.com/android/button/ put them in the drawable/ folder

here is how to show button as disabled.

 button.setBackgroundResource(R.drawable.disabled);

to show it as enabled

 button.setBackgroundResource(R.drawable.enabled);

add boolean variable, that allows us to know the state of button

boolean disabled=true ;//if the button is disabled at first

and then when you enable button change the background and the value of the boolean variable

disabled=false;
button.setBackgroundResource(android.R.enabled);

and when you disable it

disabled=true;
button.setBackgroundResource(android.R.disabled);

how to use : add this in the button click listener

  if(disabled){
 Toast.makeText(this,"Button disabled",Toast.LENGTH_SHORT).show();
}else {
//do what you want when button is enabled
}
public String yabadabado="";


//OnCreate...

//Whatever action that disables button
button.setEnabled(false);
String yabadabado ="toastmessage";

//button.onClick....
if (yabadabado.equals("toastmessage"))
{toast your message}
else
{some other action}

My Solution involves creating two buttons.

A on Button that look good and normal and has the usual on click

A Off Button that looks like the on button but turned off and has the disabled onClick.

Make both of these dynamically using java in the activity, and than switch them within a container layout in the xml file at runtime.

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