2010-09-29 47 views
3

我有一個遊戲,我最近添加了一個全球高分功能,讓很多人不高興,所以我想添加禁用它的選項。 我所做的是這樣的:在我的設置活動的看法,我增加了以下內容:當用戶取消選中啓用高分有沒有辦法以編程方式禁用特定佈局中的所有項目?

<!-- 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來禁用它,但那根本不起作用。 我應該使用視圖組還是其他? 我應該運行刷新方法來應用更改嗎?

+1

您設置setEnabled哪個視圖?你在尋找什麼樣的效果?如果你只是想隱藏整個部分,你可以在linearLayout上設置visibility = GONE。 – 2010-09-29 00:45:14

+0

我不是想要隱藏它,我希望它顯示,但作爲殘疾人(讓人們知道它在那裏,但會看到他們不能改變它) – ekatz 2010-09-29 05:45:26

回答

0

顯然正確的方法是通過使用偏好...我現在正在閱讀它,但我認爲這就是人們應該使用的,如果他們試圖實現這樣的東西 請查看以下鏈接更多相關信息:

http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html 和 developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/MyPreference.html developer.android.com/reference/android /preference/Preference.html

0

只需通過setVisibility()控制佈局的visiblity參數。您可以在三個值之間切換:visible,invisiblegone(請參閱the documentation)。

我認爲在你的情況下,最有意義的是在visiblegone之間切換。

32

將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); 
     } 
    } 
} 
+0

+1很好的答案 – mAndroid 2011-10-19 02:14:26

+0

+100 ....尼斯解決方案Dude – 2012-03-15 08:27:08

+0

謝謝,非常有用 – 2012-08-14 15:59:48

相關問題