【问题标题】:How to fix TextInputLayout passwordToggle?如何修复 TextInputLayout 密码切换?
【发布时间】:2019-10-04 09:38:43
【问题描述】:

我创建了一个带有验证的简单登录页面,但是当我想使用 passwordToggleEnabled 时,密码部分出现问题,应用程序崩溃并且不知道为什么,因为我有 0 个错误。

我的项目在 API 26 上。

我在 build gradle 里面添加了依赖:

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'

在 XML 文件中,这是我的密码输入代码:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/test2"
            android:layout_below="@id/usernameEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            app:errorEnabled="true"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Your Password"
                android:inputType="textPassword" />
        </com.google.android.material.textfield.TextInputLayout>

在 MainActivity.java 这就是我调用 pwd 的 id 的方式:

String passwordInput = test2.getText().toString().trim();

这就是我实现代码的方式,而应用程序不断停止,所以我做错了什么?

谢谢!

【问题讨论】:

  • 这是什么意思 我的项目在 API 26 上 ?同时发布你的崩溃日志
  • 对不起,我是 android studio 的新手,我不知道我可以在 logcat 中看到我的错误。
  • 调试您的应用并使用屏幕底部的 Logcat 面板

标签: java android android-textinputlayout android-support-design android-textinputedittext


【解决方案1】:

使用getEditText方法获取用于文本输入的EditText

TextInputLayout test2= findViewById(R.id.test2);
String passwordInput = test2.getEditText().getText().toString().trim();

【讨论】:

  • 谢谢!我的错误是我没有为 EditText 分配 id 现在它已解决但有一个简单的问题是切换和错误消息看起来非常接近我该如何解决这个问题? i.imgur.com/zGlDJ2h.png@Gabriele Mariotti
  • @SmithBZ 这是一个单独的问题。与你的问题无关。您可能希望考虑使用所使用的代码和一个屏幕来帮助社区提出一个单独的问题。还可以考虑将设计库迁移到material components library
  • 是的,你是对的。无论如何感谢您的帮助,非常感谢@Gabriele Mariotti
【解决方案2】:

您必须首先通过为视图分配这样的 ID 来找到视图

  <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Your Password"
                    android:inputType="textPassword" />

然后像这样在你的活动中找到它

 TextInputEditText etPassword= findViewById(R.id.etPassword);

然后你可以得到输入的文本像

String passwordInput = etPassword.getText().toString().trim();

【讨论】:

    【解决方案3】:

    为 TextInputEditText 分配一个 ID

      <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/etPassword"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="Your Password"
                        android:inputType="textPassword" />
    

    然后像这样在你的活动中找到它

    EditText etPassword= findViewById(R.id.etPassword);
    

    然后你可以得到输入的文本像

    String mPassword = etPassword.getText().toString().trim();
    

    【讨论】:

    • 和@Somesh 的答案有什么区别?
    • 大声笑,几乎一样,我没有检查他的答案,和任何人一起去!
    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    相关资源
    最近更新 更多