我不能在一个函数中运行所有的复选框

bqujaahr  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(174)
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();
                }
            }
        });
    }

}

正如你所看到的,我有很多复选框,我需要做的就是,获取复选框文本,但是如果我尝试用我以前知道的方法来做,代码会很长。我在google上看到一个人说“你可以用android:onclick method 在xml中“我也尝试过这个方法,ide没有解析这个方法。我在发送信息时出错;ide未解析。已检查方法。这是错误信息:

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

xytpbqjk1#

您应该在clicklistener中使用checkbox而不是button类:

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();
        }
    });
}

相关问题