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


当前回答

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

其他回答

是的,它可以在XML中被禁用 只是用:

<Button
android:enabled="false"
/>

您只需在活动中编写一行代码

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

当你想要启用相同的按钮只需写

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

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

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

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

 button.setEnabled(true);

快乐编码:D

有了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

你不能在你的XML中启用它或禁用它(因为你的布局是在运行时设置的),但你可以设置它是否在android:clickable活动启动时是可点击的。