如何让两个AlertDialog在Android Studio中一个接一个出现?

时间:2016-01-22 11:48:18

标签: android

我试图让两个AlertDialog两个一个接一个出现。一个用于获取用户名,另一个用于获取密码。所以,在我运行这个时,我的应用程序崩溃了。我对Android Studio很陌生。这是我的代码。

 AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
            final AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
            final EditText text = new EditText(this);

            builder1.setTitle("Username").setMessage("Please enter your name.").setView(text);
            builder1.setPositiveButton("Apply", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface di, int i) {
                    final String name_from_alert = text.getText().toString(); 
                }
            });
            // disables outside touch cancel
            builder1.setCancelable(false);
            builder1.create().show();


            // getting password
            builder2.setTitle("Password").setMessage("Please enter a password").setView(text);
            builder2.setPositiveButton("Apply", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface di, int i) {
                    final String password_from_alert = text.getText().toString();
                }
            });
            // disables outside touch cancel
            builder2.setCancelable(false);
            builder2.create().show();

感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以创建一个类,然后扩展DialogFragment类,然后覆盖其OnCreateDialog()方法,然后创建一个静态方法来创建实例并传递参数。

OnCreateDialog

 @Override
 public Dialog onCreateDialog(Bundle savedInstanceState) {
 AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
 //set title,message or view ....

 // when you done
 return dialog.create();
 }

放置参数的静态方法(静态工厂模式设计)

public static CustomAlertDialog newInstance(String title,String message) {
CustomAlertDialog customAD = new CustomAlertDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
customAD.setArguments(args);
return customAD;
}

然后你可以像这样使用它

DialogFragment dialog = CustomAlertDialog.newInstance("titulo","message");
dialog.show(/* etc.. */ );

答案 1 :(得分:0)

对话框采用自定义布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#056FB4">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true">

    <EditText
        android:id="@+id/edUsername"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:hint="Enter Username"
        android:imeOptions="actionDone"
        android:inputType="textCapSentences|textPersonName"
        android:capitalize="sentences"
        android:maxLength="20"
        android:padding="13dp"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:textColor="@color/colorWhite"
        android:textColorHint="#c2b098"
        android:textSize="12dp"
        android:textStyle="bold" />
<EditText
        android:id="@+id/edPassword"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:hint="Enter Password"
        android:imeOptions="actionDone"
        android:inputType="textPassword"
        android:capitalize="sentences"
        android:maxLength="20"
        android:padding="13dp"
        android:scrollHorizontally="false"
        android:singleLine="true"
        android:textColor="@color/colorWhite"
        android:textColorHint="#c2b098"
        android:textSize="12dp"
        android:textStyle="bold" />
    <Button
        android:text="Ok"
        android:textSize="15dp"
        android:textColor="@color/colorWhite"
        android:id="@+id/btnOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edBoxName"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:gravity="center"
        android:textAllCaps="false" />
</RelativeLayout>

Java文件:

 public void openDialog() {
    final Dialog dialog = new Dialog(MainActivity.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    final EditText edUsername = (EditText) dialog.findViewById(R.id.edUsername);
    final EditText edPassword = (EditText) dialog.findViewById(R.id.edPassword);
    Button btnDissmissDialog = (Button) dialog.findViewById(R.id.btnOk);
    btnDissmissDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (dialog != null) {
                if (dialog.isShowing()) {
                        String strUsername = edUsername.getText().toString().trim();
                        String strPasswird = edPassword.getText().toString().trim();
                        dialog.dismiss();
                }
            }
        }
    });
    dialog.show();
}