当我使用 alertDialog 函数时,如何在屏幕旋转时使用 DialogFragments 保存对话框?
How to use DialogFragments to save the dialog box while screen rotation when I'm using function for alertDialog?
这里如何引入DialogFragment或者我们如何在使用函数显示对话框时防止Dialog消失
public void alertShow(String s){
//Game Ended...!!!
android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(this);
// Setting Dialog Title
if(s.equals("You") || (s.contains("P")))
alertDialog.setTitle("Congratulations...");
else
alertDialog.setTitle("Game's Up...");
// Setting Dialog Message
if(s.equals("You") || s.contains("P"))
alertDialog.setMessage(s+" Win The Game...");
else
alertDialog.setMessage("You Lose... I Win...");
final ImageView img = new ImageView(this);
if(s.equals("You") || (s.contains("P")))
img.setImageResource(R.drawable.firecrackers);
else
img.setImageResource(R.drawable.mmm);
alertDialog.setView(img);
// Setting Dialog Cancellation
alertDialog.setCancelable(false);
// Setting Positive "Yes" Button
SessionClass sessionClass = (SessionClass)getApplicationContext();
final String str = sessionClass.getPlayer();
alertDialog.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i;
if(null != str && str.contains("2")){
i = new Intent(MainActivity2.this, Game1_1.class);
startActivity(i);
}
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
}
});
// Showing Alert Message
alertDialog.show();
}
我是 Android 的新手,所以我不知道如何将其更改为屏幕方向改变时不会消失的对话框。
使用 DialogFragment
或设置标志并在旋转后重新触发警报对话框。
已回答here。
您只需创建一个 DialogFragment class,创建一个 AlertDialog,然后在 AlertDialog 上执行您现在正在执行的所有操作。您可以在构造函数中发送 String。然后,您可以回调 Activity 以获取点击事件。
DialogFragment 类似这样的东西(这在它自己的 Java 文件中):
import android.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
public class GameDialogFragment extends DialogFragment {
String s;
public GameDialogFragment(String str) {
this.s = str;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = "";
String message = "";
if(s.equals("You") || (s.contains("P")))
title = "Congratulations...";
else
title = "Game's Up...";
if(s.equals("You") || s.contains("P"))
message = s+" Win The Game...";
else
message = "You Lose... I Win...";
final ImageView img = new ImageView(getActivity());
if(s.equals("You") || (s.contains("P")))
img.setImageResource(R.drawable.firecrackers);
else
img.setImageResource(R.drawable.mmm);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(img);
AlertDialog dialog = builder.setTitle(title)
.setMessage(message)
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity2) getActivity()).clickedYes();
}
})
.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity2) getActivity()).clickedNo();
}
})
.create();
return dialog;
}
}
然后,在 Activity 中,在您的 alertShow() 方法中显示 DialogFragment,并定义点击处理程序:
public void alertShow(String s){
new GameDialogFragment(s).show(getFragmentManager(), "");
}
public void clickedYes() {
SessionClass sessionClass = (SessionClass)getApplicationContext();
String str = sessionClass.getPlayer();
Intent i;
if(null != str && str.contains("2")){
i = new Intent(MainActivity2.this, Game1_1.class);
startActivity(i);
}
}
public void clickedNo() {
Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
这里如何引入DialogFragment或者我们如何在使用函数显示对话框时防止Dialog消失
public void alertShow(String s){
//Game Ended...!!!
android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(this);
// Setting Dialog Title
if(s.equals("You") || (s.contains("P")))
alertDialog.setTitle("Congratulations...");
else
alertDialog.setTitle("Game's Up...");
// Setting Dialog Message
if(s.equals("You") || s.contains("P"))
alertDialog.setMessage(s+" Win The Game...");
else
alertDialog.setMessage("You Lose... I Win...");
final ImageView img = new ImageView(this);
if(s.equals("You") || (s.contains("P")))
img.setImageResource(R.drawable.firecrackers);
else
img.setImageResource(R.drawable.mmm);
alertDialog.setView(img);
// Setting Dialog Cancellation
alertDialog.setCancelable(false);
// Setting Positive "Yes" Button
SessionClass sessionClass = (SessionClass)getApplicationContext();
final String str = sessionClass.getPlayer();
alertDialog.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i;
if(null != str && str.contains("2")){
i = new Intent(MainActivity2.this, Game1_1.class);
startActivity(i);
}
finish();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
}
});
// Showing Alert Message
alertDialog.show();
}
我是 Android 的新手,所以我不知道如何将其更改为屏幕方向改变时不会消失的对话框。
使用 DialogFragment
或设置标志并在旋转后重新触发警报对话框。
已回答here。
您只需创建一个 DialogFragment class,创建一个 AlertDialog,然后在 AlertDialog 上执行您现在正在执行的所有操作。您可以在构造函数中发送 String。然后,您可以回调 Activity 以获取点击事件。
DialogFragment 类似这样的东西(这在它自己的 Java 文件中):
import android.support.v7.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
public class GameDialogFragment extends DialogFragment {
String s;
public GameDialogFragment(String str) {
this.s = str;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = "";
String message = "";
if(s.equals("You") || (s.contains("P")))
title = "Congratulations...";
else
title = "Game's Up...";
if(s.equals("You") || s.contains("P"))
message = s+" Win The Game...";
else
message = "You Lose... I Win...";
final ImageView img = new ImageView(getActivity());
if(s.equals("You") || (s.contains("P")))
img.setImageResource(R.drawable.firecrackers);
else
img.setImageResource(R.drawable.mmm);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setView(img);
AlertDialog dialog = builder.setTitle(title)
.setMessage(message)
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity2) getActivity()).clickedYes();
}
})
.setNegativeButton("Main Menu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity2) getActivity()).clickedNo();
}
})
.create();
return dialog;
}
}
然后,在 Activity 中,在您的 alertShow() 方法中显示 DialogFragment,并定义点击处理程序:
public void alertShow(String s){
new GameDialogFragment(s).show(getFragmentManager(), "");
}
public void clickedYes() {
SessionClass sessionClass = (SessionClass)getApplicationContext();
String str = sessionClass.getPlayer();
Intent i;
if(null != str && str.contains("2")){
i = new Intent(MainActivity2.this, Game1_1.class);
startActivity(i);
}
}
public void clickedNo() {
Intent intent = new Intent(MainActivity2.this, MainActivity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}