由于某些用户数据而启用或禁用复选框

时间:2011-07-25 21:15:33

标签: java android checkbox

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

[编辑#3] Arash A.领先。我正在查看错误的LinearLayout以查看各种复选框。

[编辑#2]我已尝试使用OR运算符,如建议的那样,但这也不起作用。我认为我的问题是如何在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());
    }
}
}

2 个答案:

答案 0 :(得分:1)

我认为问题可能来自这句话:

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

当您收到复选框时,请尝试将其更改为:

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

答案 1 :(得分:0)

onTabChanged方法中使用此功能。

public void onTabChanged(String tag) {
    if (SKILL_TAB.equals(tag)) {
        // Do your check
        // Maybe should be OR?
        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.");
        }

    }
}