我已经创建了一个包含两个按钮的布局,下一步和上一步。在按钮之间,我生成了一些动态视图。所以当我第一次启动应用程序时,我想禁用“Previous”按钮,因为不会有任何以前的视图。我还想禁用“下一步”按钮时,没有更多的视图显示。有办法禁用按钮吗?


当前回答

有了Kotlin,

// to disable clicks
myButton.isClickable = false 

// to disable button
myButton.isEnabled = false

// to enable clicks
myButton.isClickable = true 

// to enable button
myButton.isEnabled = true

其他回答

如果你需要禁用按钮,添加这行代码。

Button button = findViewById(R.id.button)
button.setEnabled(false);

和启用按钮,只需添加这一行

 button.setEnabled(true);

快乐编码:D

在Java中,一旦你有了按钮的引用:

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

要启用/禁用按钮,您可以使用:

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

Or:

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

因为你想从一开始就禁用按钮,你可以使用button. setenabled (false);onCreate方法。 否则,从XML中,你可以直接使用:

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" />

你试过这个吗?

myButton.setEnabled(false); 

更新:感谢格温。几乎忘记了android:clickable可以在XML布局中设置,以确定一个按钮是否可以点击。

有了Kotlin,

// to disable clicks
myButton.isClickable = false 

// to disable button
myButton.isEnabled = false

// to enable clicks
myButton.isClickable = true 

// to enable button
myButton.isEnabled = true

在Java中使用setEnabled方法即可。

myButton.setEnabled(false); 

在Kotlin

myButton.enabled = false