[英]How to enable and disable Buttons in Android?
I am trying to enable and disable 4 UI buttons programmatically. 我试图以编程方式启用和禁用4个UI按钮。 And I am using Unity3D, but I can't seem to make it work.
我正在使用Unity3D,但我似乎无法使其工作。 What am I missing?
我错过了什么? My current attempt looks like this:
我目前的尝试看起来像这样:
My LinearLayout
xml file: 我的
LinearLayout
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/helpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/help"
android:visibility="visible" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/refresh" />
<com.BoostAR.Generic.TintedImageButton
android:id="@+id/screenshotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/photo" />
<com.BoostAR.Generic.LockButton
android:id="@+id/lockButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/overlayButtonMargin"
android:src="@drawable/unlocked" />
</LinearLayout>
What I did in the code: 我在代码中做了什么:
private static final int[] AUGMENTED_UI_IDS = {
R.id.refreshButton, R.id.screenshotButton, R.id.lockButton
};
private void updateAugmentedUiVisibility()
{
final int visibility =
(mShouldShowAugmentedUI ? View.VISIBLE : View.INVISIBLE);
runOnUiThread(new Runnable() {
@Override
public void run() {
for (int id : AUGMENTED_UI_IDS) {
final View view = findViewById(id);
if (view == null) {
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
} else {
Log.e(LOG_TAG, "Visibility: " + visibility);
view.setVisibility(visibility);
}
}
}
});
}
}
The statement 该声明
Log.e(LOG_TAG, "Failed to find view with ID: " + id);
gets called. 被叫。 When I did cross reference the id numbers which seem to be good.
当我交叉引用似乎很好的id号码时。
A quick explanation that might add some order to things, when you set attributes through the code, it's good to remember these: 一个快速的解释可能会增加一些顺序,当你通过代码设置属性时,记住这些是很好的:
view.setVisibility(View.INVISIBLE); // the opposite is obvious
will make the view invisible but will still occupy the space (you just won't see it) 将使视图不可见但仍将占据空间(你只是不会看到它)
view.setVisibility(View.GONE);
will collapse the view, making it both invisible and will rearrange the views around it in a manner that will occupy the space as if it was never there. 将使视图折叠,使其既不可见,又会以占据空间的方式重新排列周围的视图,就好像它永远不会存在一样。
view.setEnabled(false); // the opposite is again obvious
will make the view non responsive but in a visually understandable manner, eg, let's say you use a Switch, and after you switch it, you would like it to become unchangable, then this would be an example: 将使视图无响应但以视觉上可理解的方式,例如,假设您使用Switch,并且在切换之后,您希望它变得不可变,那么这将是一个示例:
Switch MySwitch = (Switch) someParentView.findViewById(R.id.my_switch);
MySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
MySwitch.setEnabled(false);
}
}
}
this by the way is relevant for layouts as well (to some extent). 顺便提一下,这也与布局有关(在某种程度上)。
Hope this Helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.