如何在安卓系统中启用和禁用按钮?

3

我正在尝试以编程方式启用和禁用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>

我在代码中做了什么:

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);
                    }
                }
            }
        });
    }
}

结果:

该声明

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

被调用。当我交叉参考看起来不错的id号码时。


请在此处粘贴相关代码。您所说的“打开和关闭这4个UI按钮”是什么意思?您想禁用/启用它们吗? - codeMagic
我已经添加到问题中了。谢谢,是的,我想禁用和启用它们。这就是我想做的全部。 - Filip
已编辑以尝试改善您的格式和语法。我还删除了多余的介绍性材料和“谢谢”。此外,我从pastebin导入了xml(请不要将您的问题部分链接到第三方网站)。 - Elliott Frisch
在调用 updateAugmentedUiVisibility 之前,您是否曾经使用 setContentView(that-layout-id) 或其他任何方式将其充气到视图层次结构中,以便 findViewById 可以找到它? - Eugen Pechanec
可能是重复的问题:如何禁用Android按钮? - Jared Burrows
1个回答

2

一个快速的解释可能会使事情更有条理,当你通过代码设置属性时,需要记住以下几点:

view.setVisibility(View.INVISIBLE); // the opposite is obvious

将视图设置为不可见,但仍然占用空间(你只是看不到它)。

view.setVisibility(View.GONE);

将会折叠视图,使其变得不可见,并以一种方式重新排列其周围的视图,使其占据的空间好像从未存在过。

view.setEnabled(false); // the opposite is again obvious

这将以视觉上易于理解的方式使视图无响应,例如,假设您使用了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);
            }
        }
     } 

顺便提一下,这也与布局有关(在某种程度上)。

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接