最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

passwords - android:inputType="numberPassword" and setText method not working - Stack Overflow

programmeradmin3浏览0评论

I have an edittext in the form of

<EditText
  android:id="@+id/txt_password"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:inputType="numberPassword"
  android:layout_marginStart="5dp"
  android:hint="@string/enter_password">

and I save the password as a string to a file. I want the application to fill in the password field when it first opens, and I read the password and when I call the passwordEditText.setText(password); method (for example password = "1234"), I get a fatal error. How can I fill in a numeric password to an edittext that is android:inputType="numberPassword"?

I tried passwordEditText.setText(Integer.parseInt(password)); and passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); and passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());

I have an edittext in the form of

<EditText
  android:id="@+id/txt_password"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:inputType="numberPassword"
  android:layout_marginStart="5dp"
  android:hint="@string/enter_password">

and I save the password as a string to a file. I want the application to fill in the password field when it first opens, and I read the password and when I call the passwordEditText.setText(password); method (for example password = "1234"), I get a fatal error. How can I fill in a numeric password to an edittext that is android:inputType="numberPassword"?

I tried passwordEditText.setText(Integer.parseInt(password)); and passwordEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD); and passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());

Share Improve this question asked Feb 17 at 13:12 blackmambablackmamba 1074 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Sorry, my fault When I defined the object in my view, I defined it as

EditText passwordEditText = findViewById(R.id.txt_password);

This causes confusion, and when I defined it as

passwordEditText = findViewById(R.id.txt_password);

it was fixed. However, it may still shed light on programmers who make such stupid mistakes. :)

Here is a simple Python script using the PyPDF2 library to unlock a PDF file:

import PyPDF2

def unlock_pdf(file_path, password):
    """
    Unlock a PDF file.

    Args:
        file_path (str): The path to the PDF file.
        password (str): The password to unlock the PDF file.
    """
    pdf_file = open(file_path, 'rb')
    pdf_reader = PyPDF2.PdfFileReader(pdf_file)
    if pdf_reader.isEncrypted:
        if pdf_reader.decrypt(password) == 1:
            print("PDF file unlocked successfully.")
            pdf_writer = PyPDF2.PdfFileWriter()
            for page_num in range(pdf_reader.numPages):
                pdf_writer.addPage(pdf_reader.getPage(page_num))
            with open('unlocked_' + file_path, 'wb') as f:
                pdf_writer.write(f)
        else:
            print("Incorrect password. Please try again.")
    else:
        print("The PDF file is not encrypted.")
    pdf_file.close()

Example usage:
file_path = 'path_to_your_pdf_file.pdf'
password = 'your_pdf_password'
unlock_pdf(file_path, password)

To use this script, replace 'path_to_your_pdf_file.pdf' with the path to your PDF file and 'your_pdf_password' with the password to unlock the PDF file.

Please note that you need to have the PyPDF2 library installed in your Python environment. You can install it using pip:

bash
pip install PyPDF2

Also, keep in mind that this script will create a new unlocked PDF file and will not modify the original file. The new file will be saved in the same directory as the original file, with 'unlocked_' prepended to the original file name.

发布评论

评论列表(0)

  1. 暂无评论