【问题标题】:How to use TextInputLayout in Android Studio如何在 Android Studio 中使用 TextInputLayout
【发布时间】:2021-08-29 03:29:05
【问题描述】:

这是电子邮件的 .XML 代码,密码和其他字段类似。

 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edittxt_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#f1f2f6"
            android:hint="Email">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionNext"
                android:background="@color/light_grey"
                android:inputType="textEmailAddress"
                android:typeface="sans" />

        </com.google.android.material.textfield.TextInputLayout>

   

现在,我在这样的 Java 文件中使用它。即使我显示全名,它也会显示一些存储的 id 而不是实际名称。

fullname = (TextInputLayout) findViewById(R.id.editxt_fullname);

对于注册按钮的点击,这就是我正在做的。

     btnregister.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
        
                        String user = fullname.toString`btnregister.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
        
                        String user = fullname.toString();
    ();

当我显示用户只是为了查看它存储的内容时,它会显示一些 id,而不是用户名。问题是什么?首先,我得到了转换错误,现在我得到的是无效的文本,而不是我输入的文本

【问题讨论】:

    标签: java android android-studio android-edittext android-textinputlayout


    【解决方案1】:

    这里的问题是你直接将fullname 转换为字符串使用。

    String user = fullname.toString();
    

    这是错误的,因为fullnameTextInputLayout

    要从TextInputLayout 获取文本,您需要使用:

    String user = fullname.getEditText().getText().toString();
    

    【讨论】:

      【解决方案2】:

      您可以使用 TextInputLayout 向用户显示错误。看来您想要做的是从输入字段中获取文本,即 EditText。你需要做的是给 EditText 一个 id,然后在你的 Java 代码中引用它,而不是 TextInputLayout。 因此,您可以将您的 id 从 TextInputLayout 中移出并将其放在 EditText 上,如下所示:

      <com.google.android.material.textfield.TextInputLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:background="#f1f2f6"
              android:hint="Email">
      
              <EditText
                  android:id="@+id/edittxt_email"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:imeOptions="actionNext"
                  android:background="@color/light_grey"
                  android:inputType="textEmailAddress"
                  android:typeface="sans" />
      
      </com.google.android.material.textfield.TextInputLayout>
      

      然后在你的 Java 代码中引用它。

      fullname = (EditText) findViewById(R.id.editxt_fullname);
      

      【讨论】:

        猜你喜欢
        • 2021-01-24
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 2021-09-15
        • 2020-12-01
        • 2022-01-16
        • 2016-11-20
        • 2017-09-25
        相关资源
        最近更新 更多