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

标签 android user-interface

我有一个游戏,我最近在其中添加了全局高分功能,这让很多人不高兴,所以我想添加禁用它的选项。 我所做的是:在我的设置 Activity View 中,我添加了以下内容:

<!-- 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>

我想做的是在用户取消选中启用高分跟踪复选框时禁用整个“高分特定设置”布局。 我尝试通过将 setEnabled 设置为 false 来禁用它,但这根本不起作用。 我应该使用 View 组还是什么? 是否有我应该运行的刷新方法来应用更改?

最佳答案

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

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

关于android - 有没有办法以编程方式禁用特定布局中的所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3818013/

相关文章:

android - Java如何显示一个月中的星期几

java - LibGDX HelloWorld 项目在 Android 模拟器上运行时崩溃

java - 除两个微调器值

python - 将标准输出(打印)命令打印到扩展名为 .pyw 的 tkinter 窗口

java - 在另一个线程中创建阶段时出现 IllegalStateException

java - 在屏幕上非线性滑动

java - android 单选按钮接受

node.js - 通过node.js和mongodb实现/使用quill的正确方法是什么?

java - 在并发应用程序之上构建 GUI?

java - Tic Tac Toe 游戏重置太快