我编写这段代码,在按下'OK'按钮后,出现'yes'和'no'选项的对话框。 '是'应该引导用户进行'ShowResult'活动,它将显示来自'主要活动'的用户输入; '否'只会有一个敬酒信息,说'你已经取消了提交'。意图重叠对话框?
问题是当点击'OK'按钮,对话弹出一秒钟,然后意图就会出现,覆盖对话框。
如果我点击返回并点击“是”,该活动将不会显示用户输入。这是我一直在努力的主要活动代码。我希望不要改变整个结构,因为这是一个学校项目,我们应该使用简单的技术。
Button btnOK;
EditText txtName;
EditText txtCoursename;
RadioButton rdnYear1;
RadioButton rdnYear2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOK = (Button) findViewById(R.id.btnOK);
btnOK.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, ShowResult.class);
txtName = (EditText) findViewById(R.id.txtName);
txtCoursename = (EditText) findViewById(R.id.txtCoursename);
rdnYear1 = (RadioButton) findViewById(R.id.rdnYear1);
rdnYear2 = (RadioButton) findViewById(R.id.rdnYear2);
intent.putExtra("Name", txtName.getText().toString());
intent.putExtra("Course Name", txtCoursename.getText().toString());
intent.putExtra("Year 1", rdnYear1.isChecked());
intent.putExtra("Year 2", rdnYear2.isChecked());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setCancelable(false);
switch (v.getId()) {
case R.id.btnOK:
builder.setTitle("You are submitting your data.");
builder.setMessage("Are you sure?");
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You cancelled the submission.", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(MainActivity.this, ShowResult.class));
}
});
builder.show();
}
}
}
基于您从警报对话框中的是按钮代码,您没有开始创建具有您的额外内容或字符串的意图。它应该是'startActivity(意图)' – Roljhon
刚刚尝试过starActivity(意图),但它没有改变任何东西。 – Leona