我创建了一个包含两个按钮的布局,Next和Previous。 在按钮之间,我正在生成一些动态视图。 因此,当我第一次启动应用程序时,我想禁用“上一页”按钮,因为之前没有任何视图。 我还想在没有更多要显示的视图时禁用“下一步”按钮。 无论如何禁用按钮?
#1楼
您只需在活动中编写一行代码即可
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(false);
当你想启用相同的按钮时才写
Button btn = (Button) findViewById(R.id.button1);
btn.setEnabled(true);
#2楼
就我而言,
myButton.setEnabled(false);
myButton.setEnabled(true);
工作正常,它正在启用和禁用按钮。 但是一旦按钮状态被禁用,它就不会再次返回到启用状态,尽管它是可点击的。 我尝试了无效和刷新可绘制状态,但没有运气。
myButton.invalidate();
myButton.refreshDrawableState();
如果您或任何有类似问题的人,对我有用的是再次设置背景可绘制。 适用于任何API级别。
myButton.setEnabled(true);
myButton.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.myButtonDrawable));
#3楼
在Java中,一旦你有了按钮的引用:
Button button = (Button) findviewById(R.id.button);
要启用/禁用按钮,您可以使用以下任一方法:
button.setEnabled(false);
button.setEnabled(true);
要么:
button.setClickable(false);
button.setClickable(true);
由于你想从头开始禁用按钮,你可以使用button.setEnabled(false); 在onCreate方法中。 否则,从XML,您可以直接使用:
android:clickable = "false"
所以:
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/button_text"
android:clickable = "false" />
#4楼
首先在xml中将按钮设为android:clickable="false"
android:id="@+id/btn_send"
android:clickable="false"/>
然后在你的代码中,在oncreate()方法里面设置button属性为
btn.setClickable(true);
然后在按钮内单击将代码更改为
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);
}
});
}
#5楼
是的,只需使用以下方法即可在XML中禁用它:
android:enabled="false"
/>