我无法在一个函数中运行所有复选框。

huangapple go评论73阅读模式
英文:

I can't run all off my CheckBoxes in one function

问题

public class Interests extends AppCompatActivity {

    TextView mAgeText, mGenderText;
    String strAgeText, strGenderText;
    CheckBox rugby, football, basketball, tennis, volleyball, photo, camping, bgames, painting, gardening, puzzle,
            fashion, lego, simulation, openworld, rpg, action, horror, strategy, sports, classics, mystery, fantasy, history,
            scifi, horrorbook, romance, biography, selfimp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interests);
        defVars();
        getInfo();
        // setTextViews();
    }

    public void defVars() {
        // mAgeText = findViewById(R.id.textAge);
        // mGenderText = findViewById(R.id.textGender);
        rugby = findViewById(R.id.rugby);
        football = findViewById(R.id.football);
        basketball = findViewById(R.id.basketball);
        tennis = findViewById(R.id.tennis);
        volleyball = findViewById(R.id.volleyball);
        photo = findViewById(R.id.photo);
        camping = findViewById(R.id.camping);
        bgames = findViewById(R.id.bgames);
        painting = findViewById(R.id.painting);
        gardening = findViewById(R.id.gardening);
        puzzle = findViewById(R.id.puzzle);
        fashion = findViewById(R.id.fashion);
        lego = findViewById(R.id.lego);
        simulation = findViewById(R.id.simulation);
        openworld = findViewById(R.id.openworld);
        rpg = findViewById(R.id.rpg);
        action = findViewById(R.id.action);
        horror = findViewById(R.id.horror);
        strategy = findViewById(R.id.strategy);
        sports = findViewById(R.id.sports);
        classics = findViewById(R.id.classics);
        mystery = findViewById(R.id.mystery);
        fantasy = findViewById(R.id.fantasy);
        history = findViewById(R.id.history);
        scifi = findViewById(R.id.scifi);
        horrorbook = findViewById(R.id.horrorbook);
        romance = findViewById(R.id.romance);
        biography = findViewById(R.id.biography);
        selfimp = findViewById(R.id.selfimp);
    }

    public void getInfo() {
        Bundle intent = getIntent().getExtras();
        strAgeText = intent.getString("ageText");
        strGenderText = intent.getString("genderText");
    }

    public void setTextViews() {
        // mGenderText.setText(strGenderText);
        // mAgeText.setText(strAgeText);
    }

    public void sendInfoOtherAct(final CheckBox checkBox) {
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkBox.isChecked()) {
                    Toast.makeText(Interests.this, checkBox.getText() + " checked", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(Interests.this, checkBox.getText() + " not checked", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Note: I've made the following changes in the code:

  1. Renamed the class interests to Interests to follow Java naming conventions.
  2. Changed the parameter of sendInfoOtherAct method from Button to CheckBox.
  3. Removed the commented out lines of code to improve readability.
英文:
public class interests extends AppCompatActivity {
TextView mAgeText, mGenderText;
String strAgeText, strGenderText;
CheckBox rugby, football, basketball, tennis, volleyball, photo, camping, bgames, painting, gardening, puzzle,
fashion, lego, simulation, openworld, rpg, action, horror, strategy, sports, classics, mystery, fantasy, history,
scifi, horrorbook, romance, biography, selfimp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_interests);
defVars();
getInfo();
//setTextViews();
}
public void defVars()
{
//mAgeText = findViewById(R.id.textAge);
//mGenderText = findViewById(R.id.textGender);
rugby = findViewById(R.id.rugby);
football = findViewById(R.id.football);
basketball = findViewById(R.id.basketball);
tennis = findViewById(R.id.tennis);
volleyball = findViewById(R.id.volleyball);
photo = findViewById(R.id.photo);
camping = findViewById(R.id.camping);
bgames = findViewById(R.id.bgames);
painting = findViewById(R.id.painting);
gardening = findViewById(R.id.gardening);
puzzle = findViewById(R.id.puzzle);
fashion = findViewById(R.id.fashion);
lego = findViewById(R.id.lego);
simulation = findViewById(R.id.simulation);
openworld = findViewById(R.id.openworld);
rpg = findViewById(R.id.rpg);
action = findViewById(R.id.action);
horror = findViewById(R.id.horror);
strategy = findViewById(R.id.strategy);
sports = findViewById(R.id.sports);
classics = findViewById(R.id.classics);
mystery = findViewById(R.id.mystery);
fantasy = findViewById(R.id.fantasy);
history = findViewById(R.id.history);
scifi = findViewById(R.id.scifi);
horrorbook = findViewById(R.id.horrorbook);
romance = findViewById(R.id.romance);
biography = findViewById(R.id.biography);
selfimp = findViewById(R.id.selfimp);
}
public void getInfo()
{
Bundle intent = getIntent().getExtras();
strAgeText = intent.getString("ageText");
strGenderText = intent.getString("genderText");
}
public void setTextViews()
{
mGenderText.setText(strGenderText);
mAgeText.setText(strAgeText);
}
public void sendInfoOtherAct(final Button button)
{
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (button.isChecked())
{
Toast.makeText(MainActivity.this, button.getText()+" checked", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, button.getText()+" not checked", Toast.LENGTH_SHORT).show();
}
}
});
}
}

As you can see, i have many CheckBoxes, all i need to do is, getting checked CheckBox texts, but if i try to do that with the method that i knew before, code will be so long. I googled on this and saw a guy said that "you can use android:OnClick method in xml" i also tried this and IDE didn't resolve that method. My error in sendInfoOtherAct funtion; IDE not resolving .isChecked method. That's the error message:

if (button.isChecked())
^
symbol:   method isChecked()
location: variable button of type Button

答案1

得分: 0

你应该在clickListener中使用CheckBox类,而不是Button类:

public void sendInfoOtherAct(final CheckBox checkBox) {
    checkBox.setOnClickListener(view -> {
        if (checkBox.isChecked()) {
            Toast.makeText(MainActivity.this, checkBox.getText() + " 已选中", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, checkBox.getText() + " 未选中", Toast.LENGTH_SHORT).show();
        }
    });
}
英文:

You should be using CheckBox instead of Button class in your clickListener:

public void sendInfoOtherAct(final CheckBox checkBox) {
checkBox.setOnClickListener(view -> {
if (checkBox.isChecked()) {
Toast.makeText(MainActivity.this, checkBox.getText()+" checked", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, checkBox.getText()+" not checked", Toast.LENGTH_SHORT).show();
}
});
}

huangapple
  • 本文由 发表于 2020年9月5日 22:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63755282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定