Java 如何在Android中启用和禁用按钮?

Java 如何在Android中启用和禁用按钮?,java,android,xml,user-interface,Java,Android,Xml,User Interface,我试图以编程方式启用和禁用4个UI按钮。我正在使用Unity3D,但我似乎无法让它工作。我错过了什么?我当前的尝试如下所示: 我的LinearLayoutxml文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/overlay" android:layou

我试图以编程方式启用和禁用4个UI按钮。我正在使用Unity3D,但我似乎无法让它工作。我错过了什么?我当前的尝试如下所示:

我的
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>
结果: 声明

Log.e(LOG_TAG, "Failed to find view with ID: " + id);

有人打电话来。当我交叉参考身份证号码时,看起来不错

这是一个快速的解释,可能会增加一些顺序,当您通过代码设置属性时,最好记住以下几点:

view.setVisibility(View.INVISIBLE); // the opposite is obvious
将使视图不可见,但仍会占用空间(您只是看不到)

将折叠视图,使其不可见,并以一种占用空间的方式重新排列其周围的视图,就好像它从未存在过一样

view.setEnabled(false); // the opposite is again obvious
将使视图无响应,但以视觉上可以理解的方式显示,例如,假设您使用了一个开关,在您切换它之后,您希望它变得不可更改,那么这将是一个示例:

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);
            }
        }
     } 
顺便说一下,这也与布局相关(在某种程度上)


希望这有帮助。

您应该将相关代码粘贴到此处。“打开和关闭这4个UI按钮”到底是什么意思?你想禁用/启用它们吗?我确实添加了问题。谢谢,是的,我想禁用和启用它们。这就是我想要做的。编辑以尝试改进你的格式和语法。我还删除了无关的介绍材料和感谢。此外,我还从pastebin导入了xml(请不要将部分问题链接到第三方网站)。在调用
updateaugmenteducability
call
setContentView(该布局id)之前,您是否在任何时候
或以任何其他方式将所述布局充气到视图层次结构中,以便
findViewById
可以找到它?可能的副本
view.setEnabled(false); // the opposite is again obvious
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);
            }
        }
     }