如何在 Android Studio 中使用 TextInputLayout
How to use TextInputLayout in Android Studio
这是电子邮件的 .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,而不是用户名。问题是什么?首先我遇到了转换错误,现在我得到的是无效文本而不是我输入的文本
您可以使用 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);
这里的问题是您直接将 fullname
转换为使用的字符串。
String user = fullname.toString();
这是错误的,因为 fullname
是 TextInputLayout
。
要从 TextInputLayout
获取文本,您需要使用:
String user = fullname.getEditText().getText().toString();
这是电子邮件的 .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,而不是用户名。问题是什么?首先我遇到了转换错误,现在我得到的是无效文本而不是我输入的文本
您可以使用 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);
这里的问题是您直接将 fullname
转换为使用的字符串。
String user = fullname.toString();
这是错误的,因为 fullname
是 TextInputLayout
。
要从 TextInputLayout
获取文本,您需要使用:
String user = fullname.getEditText().getText().toString();