password prompt on android

2019-06-15 06:32发布

问题:

I am trying to make a password prompt, when user entered a wrong password, it will show a dialog asking to "cancel" or "retry" and when the user clicks on "retry", it will display the password prompt again.

Below are images to illustrate what I meant

This is how I have done it

 /** 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();

            }
        });

Does anyone know how to do it?

回答1:

I have solved my own problem. I made a method for my alert dialog and then when I click on "retry", i will call the method again. :)

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();

}


回答2:

Here's a nice sample - http://www.mkyong.com/android/android-prompt-user-input-dialog-example from mkyong.

layout file for password prompt

<?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>


回答3:

Try this

 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;
}

layout:

<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" />

Use it as:

  LoginToApp loginToApp = LoginToApp.newInstance();
        loginToApp.show(getSupportFragmentManager(),LoginToApp.TAG);