我有一个游戏,我最近在其中添加了全局高分功能,这让很多人不高兴,所以我想添加禁用它的选项。 我所做的是:在我的设置 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/