我的代码存在问题。我想使用提交按钮打开下一个活动,但我遇到了问题。任何人都可以帮助我解决我所犯的错误,以便我可以实现它吗?感谢
public class Chairperson extends Activity implements View.OnClickListener{
TextView textView;
Button submit_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chairperson);
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(this);
textView = (TextView) findViewById(R.id.welcome_txt);
String message = getIntent().getStringExtra("message");
textView.setText(message);
Button submit_btn = (Button) findViewById(R.id.submit_btn);
final TextView submitTextView = (TextView) findViewById(R.id.submitTextView);
final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
submit_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the checked Radio Button ID from Radio Grou[
int selectedRadioButtonID = rg1.getCheckedRadioButtonId();
// If nothing is selected from Radio Group, then it return -1
if
(selectedRadioButtonID != -1) {
RadioButton selectedRadioButton = (RadioButton) findViewById(selectedRadioButtonID);
String selectedRadioButtonText = selectedRadioButton.getText().toString();
submitTextView.setText(selectedRadioButtonText + " selected.");
} else {
submitTextView.setText("Nothing selected .");
}
}
});
}
@Override
public void onClick(View v) {
startActivity(new Intent(this, ViceChairperson.class));
}
}
答案 0 :(得分:0)
我已经为您的按钮编写了代码,删除了代码中所有以前的submit_btn代码并替换为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addListenerOnButton();
public void addListenerOnButton() {
final Context context = this;
submit_btn = (Button) findViewById(R.id.submit_btn);
submit_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (radioGroup.getCheckedRadioButtonId() == -1)
{
Toast.makeText(context, "Select an option.", Toast.LENGTH_LONG).show();
}
else{
Intent intent = new Intent(context, ViceChairperson.class);
startActivity(intent);
finish();
}
}
});
}
}
如果您有任何问题,请告诉我。
答案 1 :(得分:0)
移动线
startActivity(new Intent(getApplicationContext(), ViceChairperson.class));
在if (selectedRadioButtonID != -1)
检查后。
如果检查成功则启动新活动,如果没有,则不启动任何活动
不需要第二个onClick
方法,它不受任何约束,永远不会被调用。