How to disable an Android button?

2019-01-01 03:43发布

问题:

I have created a layout that contains two buttons, Next and Previous. In between the buttons I\'m generating some dynamic views. So when I first launch the application I want to disable the \"Previous\" button since there wont be any previous views. I also want to disable the \"Next\" button when there are not more views to display. Is there anyway to disable the buttons?

\"screen

回答1:

Did you try this?

myButton.setEnabled(false); 

Update: Thanks to Gwen. Almost forgot that android:clickable can be set in your XML layout to determine whether a button can be clickable or not.



回答2:

You can\'t enable it or disable it in your XML (since your layout is set at runtime), but you can set if it\'s clickable at the launch of the activity with android:clickable.



回答3:

You just write a single line of code in your activity

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);

When you want to enable the same button just write

Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(true);


回答4:

In Java, once you have the reference of the button:

Button button = (Button) findviewById(R.id.button);

To enable/disable the button, you can use either:

button.setEnabled(false);
button.setEnabled(true);

Or:

button.setClickable(false);
button.setClickable(true);

Since you want to disable the button from the beginning, you can use button.setEnabled(false); in the onCreate method. Otherwise, from XML, you can directly use:

android:clickable = \"false\"

So:

<Button
        android:id=\"@+id/button\"
        android:layout_height=\"wrap_content\"
        android:layout_width=\"wrap_content\"
        android:text=\"@string/button_text\"
        android:clickable = \"false\" />


回答5:

Yes it can be disabled in XML just using <Button android:enabled=\"false\" />



回答6:

In my case,

myButton.setEnabled(false);
myButton.setEnabled(true);

is working fine and it is enabling and disabling the button as it should. But once the button state becomes disabled, it never goes back to the enabled state again, although it\'s clickable. I tried invalidating and refreshing the drawable state, but no luck.

myButton.invalidate();
myButton.refreshDrawableState();

If you or anyone having a similar issue, what works for me is setting the background drawable again. Works on any API Level.

myButton.setEnabled(true);
myButton.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.myButtonDrawable));


回答7:

In Kotlin, if you refer the Button View with id then, enable/disable button as like

layout.xml

<Button
   android:id=\"@+id/btn_start\"
   android:layout_width=\"100dp\"
   android:layout_height=\"50dp\"
   android:text=\"@string/start\"
   android:layout_alignParentBottom=\"true\"/>

activity.kt

  btn_start.isEnabled = true   //to enable button
  btn_start.isEnabled = false  //to disable button


回答8:

first in xml make the button as android:clickable=\"false\"

<Button
        android:id=\"@+id/btn_send\"
        android:clickable=\"false\"/>

then in your code, inside oncreate() method set the button property as

btn.setClickable(true);

then inside the button click change the code into

btn.setClickable(false);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    btnSend = (Button) findViewById(R.id.btn_send);
    btnSend.setClickable(true);
    btnSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            btnSend.setClickable(false);

        }
    });
}


回答9:

WRONG WAY IN LISTENER TO USE VARIABLE INSTEAD OF PARAMETER!!!

btnSend.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSend.setClickable(false);

    }
});

RIGHT WAY:

btnSend.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        /** check given view  by assertion or cast as u wish */
        if(v instance of Button) {

            /** cast */
            Button button = (Button) v;

            /** we can perform some check up */
            if(button.getId() == EXPECTED_ID) {

                /** disable view */
                button.setEnabled(false)            
                button.setClickable(false); 
            }

        } else {

             /** you can for example find desired view by root view  */
             Button bt = (Button) v.getRootView().findViewById(R.id.btId);

             /*check for button */
             if(bt!=null) {

                 /** disable button view */
                 ...
             } else {
                 /** according to @jeroen-bollen remark
                   * we made assumption that we expected a view
                   * of type button here in other any case  
                   */
                  throw new IllegalArgumentException(\"Wrong argument: \" +
                         \"View passed to method is not a Button type!\");
             }
          }
       }
    });

EDIT: In reply to @jeroen-bollen

 View.OnClickListener 

is Interface definition for a callback to be invoked when a view is clicked.

with method definition

void onClick(View v);

when the view is clicked the View class object makes callback to method onClick() sending as parameter itself, so null view parameter should not occur if it does it\'s an Assertion Error it could happen for example when View object class was destroyed in meanwhile (for example collected by GC) or method was tampered due to hack

little about instanceof & null

JLS / 15.20.2. Type Comparison Operator instanceof

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException.

Otherwise the result is false.


three words from the Author

IF U ASK WHY ?

MOSTLY TO AVOID NullPointerException

Little more code will save your time on later bug tracking in your code & reduces the occurrence of abnomalies.

consider following example:

View.OnClickListener listener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSend.setClickable(false);

    }
});

btnSend.setOnClickListener(listener)
btnCancel.setOnClickListener(listener)