按下后设置密码

时间:2018-03-10 04:54:38

标签: android passwords alert

我跟着this提示警告密码

所以我已经给予了这一点,但它没有工作

 @Override
public void onBackPressed() {
 showDialog();
}

但它没有工作onBackPressed它返回或退出应用程序......

  

如何使用onBackPressed提醒

工作正常
@Override
public void onBackPressed() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setTitle("Do You Want to Exit ?");
    alertDialogBuilder
            .setMessage("Click Yes to Exit!")
            .setCancelable(false)
            .setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);
                        }
                    })

            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}
  

但我想要提示密码onBackPressed

任何人都可以帮我解决onBackPressed的密码错误吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

     @Override
    public void onBackPressed() {
        if (isFirsttym) {
            super.onBackPressed();
            return;
        }

//        this.isFirsttym = true;
        showDialog();

//        new Handler().postDelayed(new Runnable() {
//
//            @Override
//            public void run() {
//                isFirsttym=false;
//            }
//        }, 5000);
    }


    public void showDialog()
    {

        LayoutInflater li = LayoutInflater.from(this);
        View promptsView = li.inflate(R.layout.cust_dialog, null);
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        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);
                                    isFirsttym=true;

                                }
                                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(HomeActivity.this);
                                    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();
                                            isFirsttym=false;

                                        }
                                    });
                                    builder.create().show();

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

                        }

                );

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

        // show it
        alertDialog.show();

    }

答案 1 :(得分:1)

嗨,一旦尝试过这对我来说很好......

提供直接提醒而不是布局

@Override
public void onBackPressed() {

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Title");
    alert.setMessage("Message");

    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString();
            // Do something with value!


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

            }
            else{
                //Log.d(input,"string is empty");
                String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                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();

            }
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    alert.show();

}