gpt4 book ai didi

java - 根据某些用户数据启用或禁用复选框

转载 作者:行者123 更新时间:2023-12-01 15:47:40 30 4
gpt4 key购买 nike

我正在尝试禁用我拥有的 TabHost 中的一些复选框。当应用加载时,它应该检查某些用户数据(在 characterData.getCharacterClass()characterData.getCharacterRace() 中找到)是否为空,如果是则禁用复选框在“技能”选项卡中。遗憾的是,它不会禁用复选框(当 characterData.getCharacterClass()characterData.getCharacterRace() 为 true 或以其他方式包含数据时,它应该启用它们)。我尝试设置一个 OnFocusChangedListener() 来在焦点切换到“技能”选项卡时检查此数据,但正如您可能看到的那样,这不起作用。

[编辑 #3] Arash A. 领先。我正在寻找错误的 LinearLayout 来浏览各个复选框。

[编辑 #2] 我已按照建议尝试使用 OR 运算符,但这也不起作用。我认为我的问题是如何浏览 changeSkillCheckboxes() 函数中的 View 。我认为这是因为我只提出了两个 subview ,而不是所有必要的复选框。现在我需要做的就是弄清楚如何实际获取所有复选框。

[编辑 #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());
}
}
}

最佳答案

我认为问题可能出在这句话上:

CheckBox cb = (CheckBox) skillsCheckboxLayout.getChildAt(i);

当您获得复选框时,尝试将其更改为:

CheckBox cb = (CheckBox)findViewById(R.id.checkbox1);

关于java - 根据某些用户数据启用或禁用复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6822462/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com