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

时间:2015-06-05 15:21:04

标签: java android xml user-interface

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

1 个答案:

答案 0 :(得分: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);
            }
        }
     } 
顺便提一下,这也与布局有关(在某种程度上)。

希望这有助于。