我試圖禁用TabHost
中的某些複選框。當應用程序加載時,它應檢查以查看某些用戶數據(在characterData.getCharacterClass()
和characterData.getCharacterRace()
中找到)是否爲空,如果是,則禁用「技能」選項卡中的複選框。可悲的是,它不會禁用複選框(當characterData.getCharacterClass()
和characterData.getCharacterRace()
爲真或包含數據時,它應該啓用它們)。我已經嘗試設置OnFocusChangedListener()
以在焦點切換到「技能」選項卡時檢查這些數據,但您可能會發現這不起作用。由於某些用戶數據,啓用或禁用複選框
[編輯#3] Arash A.有很好的領先優勢。我正在查看錯誤的LinearLayout來移動各種複選框。
[編輯#2]我曾嘗試使用或運算符,建議,但也不起作用。我認爲我的問題是我如何瀏覽我的changeSkillCheckboxes()
函數中的視圖。我認爲這是因爲我只想出兩個子視圖,而不是所有必要的複選框。現在我需要做的是弄清楚如何實際獲得所有的複選框。
[編輯#1]我試過給我的第一個建議。但是那也行不通。也許它是在假設禁用複選框的方法中。下面是該代碼:
public void changeSkillCheckboxes(boolean toggle) {
if (toggle) {
for (int i = 0; i < skillsCheckboxLayout.getChildCount(); i++) {
if (skillsCheckboxLayout.getChildAt(i) instanceof CheckBox) {
CheckBox cb = (CheckBox) skillsCheckboxLayout.getChildAt(i);
cb.setEnabled(false);
Log.i(DEBUG_LOG, "Box disabled.");
} else {
Log.i(DEBUG_LOG, "CheckBox wasn't found, with count " + i);
}
}
} else {
for (int i = 0; i < skillsCheckboxLayout.getChildCount(); i++) {
if (skillsCheckboxLayout.getChildAt(i) instanceof CheckBox) {
CheckBox cb = (CheckBox) skillsCheckboxLayout.getChildAt(i);
cb.setEnabled(true);
}
}
}
}
代碼檢查,看看是否有什麼被改變的複選框,否則就會拋出一個錯誤(我也有一個滾動的佈局在那裏)。
任何方式,下面是兩個類的代碼:
標籤文件
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TableLayout;
import android.widget.TabHost.TabContentFactory;
public class CreateCharacterTabsActivity extends Activity implements OnTabChangeListener {
private static final String DEBUG_LOG = "DnDAppDebugInfo";
private TabHost tabHost;
private TableLayout scoreTable;
private LinearLayout raceLayout;
private LinearLayout characterClassesLayout;
private LinearLayout skillLayout;
private CharacterAbilityScoresActivity abilityScoresActivity;
private CharacterRaceActivity raceActivity;
private SkillChooserActivity skillActivity;
// Tab tags: got to love them.
private static final String ABILITY_SCORES_TAB = "scores";
private static final String RACE_TAB = "race";
private static final String CLASS_TAB = "classes";
private static final String SKILL_TAB = "skills";
CharacterData characterData = new CharacterData();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs);
// Set up them tabs.
tabHost = (TabHost) findViewById(R.id.tabhost01);
// Set up the tabhost.
tabHost.setup();
tabHost.setOnTabChangedListener(this);
// Ability scores
abilityScoresActivity = new CharacterAbilityScoresActivity(this) ;
scoreTable = (TableLayout) abilityScoresActivity.getAbilityScoreTable();
// Character race
raceActivity = new CharacterRaceActivity(this);
raceLayout = (LinearLayout) raceActivity.getRacesLayout();
// Character class
characterClassesLayout = (LinearLayout) (new CharacterClassActivity(this)).getCharacterClassLayout();
// Class skills
skillActivity = new SkillChooserActivity(this);
skillLayout = (LinearLayout) skillActivity.getSkillsCheckboxLayout();
/* Check to see if the user has selected a race and class. Once they have enable the skills checkboxes. */
// tabHost.getTabWidget().getChildAt(3)
skillLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (characterData.getCharacterClass() == null && characterData.getRace() == null) {
skillActivity.changeSkillCheckboxes(true); // Disable the skill checkboxes
Log.i(DEBUG_LOG, "Skill checkboxes disabled.");
} else {
skillActivity.changeSkillCheckboxes(false); // Enable the skill checkboxes
Log.i(DEBUG_LOG, "Skill checkboxes enabled.");
}
}
}
);
// Add views to the tab host.
tabHost.addTab(tabHost.newTabSpec(ABILITY_SCORES_TAB).setIndicator("Scores").setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return scoreTable;
}
}));
tabHost.addTab(tabHost.newTabSpec(RACE_TAB).setIndicator("Races").setContent(new TabContentFactory() {
public View createTabContent(String arg1) {
return raceLayout;
}
}));
tabHost.addTab(tabHost.newTabSpec(CLASS_TAB).setIndicator("Classes").setContent(new TabContentFactory() {
public View createTabContent(String arg2) {
return characterClassesLayout;
}
}));
tabHost.addTab(tabHost.newTabSpec(SKILL_TAB).setIndicator("Skills").setContent(new TabContentFactory() {
public View createTabContent(String arg3) {
return skillLayout;
}
}
));
// I have heard this is a hack brought upon by a bug.
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);
}
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
}
}
技能文件
package com.androidGuy.DnDApp;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
public class SkillChooserActivity extends Activity implements OnClickListener {
// Character information
CharacterData characterData = new CharacterData();
LinearLayout skillsCheckboxLayout;
Context leContext;
private static final String DEBUG_LOG = "DnDAppDebugInfo";
// Skill checkbox member variables
private CheckBox acrobaticsCheckBox;
private CheckBox arcanaCheckBox;
private CheckBox athleticsCheckBox;
private CheckBox bluffCheckBox;
private CheckBox diplomacyCheckBox;
private CheckBox dungeoneeringCheckBox;
private CheckBox enduranceCheckBox;
private CheckBox healCheckBox;
private CheckBox historyCheckBox;
private CheckBox insightCheckBox;
private CheckBox intimidateCheckBox;
private CheckBox natureCheckBox;
private CheckBox perceptionCheckBox;
private CheckBox religionCheckBox;
private CheckBox stealthCheckBox;
private CheckBox streetwiseCheckBox;
private CheckBox thieveryCheckBox;
SkillChooserActivity(Context mContext) {
leContext = mContext;
LayoutInflater inflater = (LayoutInflater) leContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
skillsCheckboxLayout = (LinearLayout) inflater.inflate(R.layout.other_skill_selector, null);
/* And now, lots and lots of checkboxes. */
acrobaticsCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.AcrobaticsCheckBox);
acrobaticsCheckBox.setOnClickListener(this);
arcanaCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.ArcanaCheckBox);
arcanaCheckBox.setOnClickListener(this);
athleticsCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.AthleticsCheckBox);
athleticsCheckBox.setOnClickListener(this);
bluffCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.BluffCheckBox);
bluffCheckBox.setOnClickListener(this);
diplomacyCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.DiplomacyCheckBox);
diplomacyCheckBox.setOnClickListener(this);
dungeoneeringCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.DungeoneeringCheckBox);
dungeoneeringCheckBox.setOnClickListener(this);
enduranceCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.EnduranceCheckBox);
enduranceCheckBox.setOnClickListener(this);
healCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.HealCheckBox);
healCheckBox.setOnClickListener(this);
historyCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.HistoryCheckBox);
historyCheckBox.setOnClickListener(this);
insightCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.InsightCheckBox);
insightCheckBox.setOnClickListener(this);
intimidateCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.IntimidateCheckBox);
intimidateCheckBox.setOnClickListener(this);
natureCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.NatureCheckBox);
natureCheckBox.setOnClickListener(this);
perceptionCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.PerceptionCheckBox);
perceptionCheckBox.setOnClickListener(this);
religionCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.ReligionCheckBox);
religionCheckBox.setOnClickListener(this);
stealthCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.StealthCheckBox);
stealthCheckBox.setOnClickListener(this);
streetwiseCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.StreetwiseCheckBox);
streetwiseCheckBox.setOnClickListener(this);
thieveryCheckBox = (CheckBox) skillsCheckboxLayout.findViewById(R.id.ThieveryCheckBox);
thieveryCheckBox.setOnClickListener(this);
}
public void changeSkillCheckboxes(boolean toggle) {
if (toggle) {
for (int i = 0; i < skillsCheckboxLayout.getChildCount(); i++) {
if (skillsCheckboxLayout.getChildAt(i) instanceof CheckBox) {
CheckBox cb = (CheckBox) skillsCheckboxLayout.getChildAt(i);
cb.setEnabled(false);
Log.i(DEBUG_LOG, "Box disabled.");
} else {
Log.i(DEBUG_LOG, "CheckBox wasn't found, with count " + i);
}
}
} else {
for (int i = 0; i < skillsCheckboxLayout.getChildCount(); i++) {
if (skillsCheckboxLayout.getChildAt(i) instanceof CheckBox) {
CheckBox cb = (CheckBox) skillsCheckboxLayout.getChildAt(i);
cb.setEnabled(true);
}
}
}
}
public LinearLayout getSkillsCheckboxLayout() {
return skillsCheckboxLayout;
}
/* Check their state. */
public void onClick(View v) {
if(((CheckBox)v).isChecked()) {
Toast.makeText(leContext, "You selected " + ((TextView)v).getText().toString(), Toast.LENGTH_SHORT).show();
Log.i(DEBUG_LOG, "com.anddroidguy.SkillChooserActivity: the value of \'Class\' is " + characterData.getCharacterClass());
} else {
Toast.makeText(leContext, "Okay, I guess you can select something else...", Toast.LENGTH_SHORT).show();
Log.i(DEBUG_LOG, "com.anddroidguy.SkillChooserActivity: the value of \'Class\' is " + characterData.getCharacterClass());
}
}
}
這是解決方案的一部分,Arash A.我在xml文件中查看了錯誤的LinearLayout。 – Ertain