英文:
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:
- Renamed the class
interests
toInterests
to follow Java naming conventions. - Changed the parameter of
sendInfoOtherAct
method fromButton
toCheckBox
. - 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();
}
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论