有没有办法以编程方式禁用特定布局中的所有项目?

eka*_*atz 3 user-interface android

我有一个游戏,我最近添加了一个全局高分功能,这让很多人感到不安,所以我想添加禁用它的选项.我做的是这样的:在我的设置活动视图中,我添加了以下内容:

<!-- High Score Tracking -->
 <LinearLayout android:layout_weight="40"
  android:layout_width="fill_parent" android:layout_height="wrap_content"
  android:orientation="vertical" android:padding="5dip">
  <LinearLayout android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <CheckBox android:text="@string/EnableHighscoreCBText"
    android:id="@+id/EnableHighscoreCB" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
   </CheckBox>
  </LinearLayout>
  <!-- High score specific settings -->
  <LinearLayout android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:orientation="horizontal"
   android:weightSum="100" android:padding="5dip">
   <CheckBox android:text="@string/EnableShareScoresCBText"
    android:id="@+id/EnableShareScoresCB" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
   </CheckBox>

   <TextView android:id="@+id/DefaultPlayerNameTv"
    android:layout_width="wrap_content" android:layout_weight="30"
    android:layout_height="wrap_content" android:text="@string/pDefName"
    android:textSize="18sp">
   </TextView>
   <EditText android:id="@+id/PlayerNameEt"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:text="@string/pNameDefVal" android:layout_weight="70"
    android:textSize="18sp" android:maxLength="20">
   </EditText>
  </LinearLayout>
 </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我想要做的是当用户取消选中启用高分记跟踪复选框时,禁用整个"高分特定设置"布局.我尝试通过将其设置setEnabled为false来禁用它,但这根本不起作用.我应该使用视图组还是其他什么?我应该运行刷新方法来应用更改吗?

ben*_*ort 34

将View.OnClickListener添加到CheckBox,然后将要禁用的视图传递给以下函数...

private void enableDisableView(View view, boolean enabled) {
    view.setEnabled(enabled);

    if ( view instanceof ViewGroup ) {
        ViewGroup group = (ViewGroup)view;

        for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
            enableDisableView(group.getChildAt(idx), enabled);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)