java 使按钮被选中_java – 让我的单选按钮在Android中被选中

当我运行时,可以单击对话框,我的单选按钮不会像预期的那样被选中

package edu.elon.cs.mobile;

public class PTCalculator extends Activity{

private RadioButton maleRadioButton;

private RadioButton femaleRadioButton;

private EditText ageEdit;

private EditText pushUpsEdit;

private EditText sitUpsEdit;

private EditText mileMinEdit;

private EditText mileSecEdit;

private Button calculate;

private TextView score;

protected AlertDialog genderAlert;

private int currScore;

private int age;

private int sitUps;

private int runTime;

private int pushUps;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.pt);

maleRadioButton = (RadioButton) findViewById(R.id.male);

femaleRadioButton = (RadioButton) findViewById(R.id.female);

ageEdit = (EditText) findViewById(R.id.ageEdit);

pushUpsEdit = (EditText) findViewById(R.id.pushupEdit);

sitUpsEdit = (EditText) findViewById(R.id.situpEdit);

mileMinEdit = (EditText) findViewById(R.id.minEdit);

mileSecEdit = (EditText) findViewById(R.id.secEdit);

calculate = (Button) findViewById(R.id.calculateButton);

calculate.setOnClickListener(calculateButtonListener);

score = (TextView) findViewById(R.id.scoreView);

genderAlert = makeGenderDialog().create();

}

private OnClickListener calculateButtonListener = new OnClickListener() {

@Override

public void onClick(View arg0) {

age = (Integer.parseInt(ageEdit.getText().toString()));

pushUps = (Integer.parseInt(pushUpsEdit.getText().toString()));

sitUps = (Integer.parseInt(sitUpsEdit.getText().toString()));

int min = (Integer.parseInt(mileMinEdit.getText().toString())*60);

int sec = (Integer.parseInt(mileSecEdit.getText().toString()));

runTime = min + sec;

if(maleRadioButton.isChecked()){

MalePTTest mPTTest = new MalePTTest(age, pushUps, sitUps, runTime);

currScore = mPTTest.malePTScore();

score.setText((Integer.toString(currScore)));

}else if(femaleRadioButton.isChecked()){

FemalePTTest fPTTest = new FemalePTTest(age, pushUps, sitUps, runTime);

currScore = fPTTest.femalePTScore();

score.setText((Integer.toString(currScore)));

}else

genderAlert.show();

}

};

public AlertDialog.Builder makeGenderDialog(){

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setMessage("Select a Gender")

.setCancelable(false)

.setPositiveButton("Female", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

femaleRadioButton.setSelected(true);

FemalePTTest fPTTest = new FemalePTTest(age, pushUps, sitUps, runTime);

currScore = fPTTest.femalePTScore();

score.setText((Integer.toString(currScore)));

}

})

.setNegativeButton("Male", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int id) {

maleRadioButton.setSelected(true);

MalePTTest mPTTest = new MalePTTest(age, pushUps, sitUps, runTime);

currScore = mPTTest.malePTScore();

score.setText((Integer.toString(currScore)));

}

});

return builder;

}

}

有什么建议?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值