12

我正在尝试进行密码提示,当用户输入错误密码时,它会显示一个对话框,要求“取消”或“重试”,当用户单击“重试”时,它将再次显示密码提示。

下面是图片来说明我的意思

在此处输入图像描述

在此处输入图像描述

这就是我的做法

 /** RETRIEVE VIEW FROM DIALOGPROMPT.XML AND SET VIEW AS AN ALERTDIALOG BUILDER **/
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.searchprompt, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.user_input);


                // set dialog message
                alertDialogBuilder
                    .setCancelable(false)
                    .setNegativeButton("Go",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            /** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
                            String user_text = (userInput.getText()).toString();

                            /** CHECK FOR USER'S INPUT **/
                            if (user_text.equals("oeg"))
                            {
                                Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
                                Search_Tips(user_text); 

                            }
                            else{
                                Log.d(user_text,"string is empty");
                                String message = "The password you have entered is incorrect." + " \n" + "Please try again";
                                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                                builder.setTitle("Error");
                                builder.setMessage(message);
                                builder.setPositiveButton("Cancel", null);
                                builder.setNegativeButton("Retry", null);
                                builder.create().show();

                            }
                            }
                      })
                    .setPositiveButton("Cancel",
                      new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                        }
                      });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });

有谁知道该怎么做?

4

3 回答 3

12

我已经解决了我自己的问题。我为我的警报对话框创建了一个方法,然后当我单击“重试”时,我将再次调用该方法。:)

public void showDialog()
{

    LayoutInflater li = LayoutInflater.from(context);
    View promptsView = li.inflate(R.layout.searchprompt, null);
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    alertDialogBuilder.setView(promptsView);

    final EditText userInput = (EditText) promptsView
            .findViewById(R.id.user_input);


    // set dialog message
    alertDialogBuilder
        .setCancelable(false)
        .setNegativeButton("Go",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                /** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
                String user_text = (userInput.getText()).toString();

                /** CHECK FOR USER'S INPUT **/
                if (user_text.equals("oeg"))
                {
                    Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
                    Search_Tips(user_text); 

                }
                else{
                    Log.d(user_text,"string is empty");
                    String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setTitle("Error");
                    builder.setMessage(message);
                    builder.setPositiveButton("Cancel", null);
                    builder.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
                        @Override
                       public void onClick(DialogInterface dialog, int id) {
                            showDialog();
                       }
                   });
                    builder.create().show();

                }
                }
          })
        .setPositiveButton("Cancel",
          new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
            dialog.dismiss();
            }

          }

        );

    // create alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // show it
    alertDialog.show();

}
于 2012-10-02T04:12:11.750 回答
4

这是一个很好的示例 - mkyong 的http://www.mkyong.com/android/android-prompt-user-input-dialog-example

密码提示的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type Your Message : "
        android:labelFor="@+id/editTextDialogUserInput"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword" >

        <requestFocus />

    </EditText>

</LinearLayout>
于 2016-07-27T15:07:32.610 回答
0

尝试这个

 public class LoginToApp extends DialogFragment {

public static final String TAG = "LoginDialog";

public static LoginToApp newInstance() {
    LoginToApp login = new LoginToApp();
    login.setStyle(STYLE_NO_TITLE, 0);
    login.setCancelable(false);
    return login;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.dialog_login_to_app, container, false);


    final EditText passwordEditText = view.findViewById(R.id.dialog_login_password);
    Button loginButton = view.findViewById(R.id.dialog_login_loginBtn);

    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String password = passwordEditText.getText().toString();
            if (getActivity() != null) {
                CharSequence passwordFromShared;
                passwordFromShared = "Your password";

                assert passwordFromShared != null;
                if (password.contains(passwordFromShared) && password.length()==passwordFromShared.length()) {
                    LoginToApp.super.dismiss();
                } else {

                    //show some info when error

                }

            }
        }
    });

    return view;
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal|center"
    android:layout_marginBottom="10dp"
    android:background="@color/colorPrimary"
    android:foregroundGravity="center_vertical|center_horizontal|center"
    android:gravity="center_vertical|center_horizontal|center"
    android:padding="10dp"
    android:text="Login"
    android:textColor="#000000"
    android:textSize="12sp"
    android:textStyle="bold" />

<EditText
    android:id="@+id/dialog_login_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Password"
    android:inputType="textPassword" />

<Button
    android:id="@+id/dialog_login_loginBtn"
    style="@style/Widget.AppCompat.Button.Borderless"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:background="@color/colorAccent"
    android:fontFamily="sans-serif"
    android:text="login"
    android:textColor="#000000" />

将其用作:

  LoginToApp loginToApp = LoginToApp.newInstance();
        loginToApp.show(getSupportFragmentManager(),LoginToApp.TAG);
于 2019-05-17T20:55:38.173 回答