我遇到了一个问题,其中AlertDialog按钮没有显示第二个屏幕的正确消息。
预期结果:第一个屏幕在AlertDialog和。中显示消息AAAAAAAAAAA 第二个屏幕应显示差异消息“CCCCCCC”
问题:对于第二个屏幕,它显示与第一个屏幕相同的消息,即使消息已在java类中更改。
以下是我的Tuto2Activity.java
public class Tuto2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuto2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void open(View view){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("How to Use");
alertDialogBuilder.setMessage("Provide a description about One BAJA in term of how to use it and benefits using it");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public void onRadioButtonClicked(View view) {
boolean checked = ( (RadioButton) view). isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked)
setContentView(R.layout.activity_tuto2);
break;
case R.id.radioButton2:
if (checked)
setContentView(R.layout.activity_tuto3);
break;
case R.id.radioButton3:
if (checked)
setContentView(R.layout.activity_tuto4);
break;
case R.id.radioButton4:
if (checked)
setContentView(R.layout.activity_tuto5);
break;
case R.id.radioButton5:
if (checked)
setContentView(R.layout.activity_tuto6);
}
}
}
Tuto3Activity.java:
public class Tuto3Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tuto3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void open(View view){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Tuto3Activity.this);
alertDialogBuilder.setTitle("How to Use");
alertDialogBuilder.setMessage("Show the list type of vegetables and flowers and information care");
alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg1, int arg2) {
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
public void onRadioButtonClicked(View view) {
boolean checked = ( (RadioButton) view). isChecked();
switch (view.getId()) {
case R.id.radioButton1:
if (checked)
setContentView(R.layout.activity_tuto2);
break;
case R.id.radioButton2:
if (checked)
setContentView(R.layout.activity_tuto3);
break;
case R.id.radioButton3:
if (checked)
setContentView(R.layout.activity_tuto4);
break;
case R.id.radioButton4:
if (checked)
setContentView(R.layout.activity_tuto5);
break;
case R.id.radioButton5:
if (checked)
setContentView(R.layout.activity_tuto6);
}
}
}
答案 0 :(得分:0)
您可以尝试使用onClickListener而不是onClick(在xml中)。
Button myButton = (Button)findViewById(R.id.myButtonID);
myButton .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Alert dialog codes...
}
});