首页 > 解决方案 > 如何从警报对话框界面传递用户输入的密码以打开受密码保护的 pdf 文件?

问题描述

barteksc在我的 pdf 应用程序中使用,我想将用户从警报对话框中输入的密码值传递setPositiveButton给“.password()”,以打开受密码保护的 pdf 文件。在onError侦听器中,我正在调用打开AlertDialog

.onError(new OnErrorListener() {
                @Override
                public void onError(Throwable t) {
                    if (t instanceof PdfPasswordException){
                       showPasswordDialog();
                    }
                }
            })

这是方法的代码

private void showPasswordDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Password:")
            .setIcon(R.drawable.ic_key)
            .setMessage("Enter Password to open pdf");

    final EditText input = new EditText(PdfActivity.this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
            (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(layoutParams);
    builder.setView(input);

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //implement cancel button
            dialogInterface.cancel();
        }
    })
    builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //implement a check that the password is correct
        }
    });
    builder.show();
}

标签: javaandroid

解决方案


推荐阅读