如何正确从Material Design TextInputLayout获取文本?

问题描述 投票:0回答:6

我想使用自定义结束图标从材料设计的 TextInputLayout 获取文本。

我尝试这样做:

TextInputLayout textInputCustomEndIcon = findViewById(R.id.editText);
final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon.getContext());
    
textInputCustomEndIcon.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (editText.getText() != null) {
            String text = editText.getText().toString();
    
            Log.i("MainActivity", "setEndIconOnClickListener:"+ text);
        }
    }
});

但是我得到一个空文本,不是空而是空!!

android material-design android-textinputlayout android-textinputedittext
6个回答
18
投票

使用类似的东西:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/custom_end_icon"
    ...>

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

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

只需使用:

TextInputLayout textInputLayout = findViewById(R.id.custom_end_icon);
String text = textInputLayout.getEditText().getText();

您的问题在这里:

final TextInputEditText editText = new TextInputEditText(textInputCustomEndIcon.getContext());

您只是创建了

TextInputEditText
的新实例,它不是
TextInputEditText
中的
TextInputLayout


1
投票

我也遇到了和你一样的问题。我就是这样解决的。在字符串变量中,我需要验证数据是否已使用 .trim()

正确插入
  String vname=Name.getEditText().getText().toString().trim();

1
投票

就我而言,您不能仅使用

String text = textInputLayout.getEditText().getText();
TextInputEditText
获取文本值。
如果您要将其包装为
Editable
,则需要将其转换为
String.valueOf()
或使用
String
来包装它,如下所示:

XML

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/request_title"
android:inputType="textCapWords"
android:maxLength="255" />
</com.google.android.material.textfield.TextInputLayout>

Java

TextInputLayout textInputLayout = findViewById(R.id.edt_title);

String strTitle = String.valueOf(textInputLayout.getEditText().getText());

Editable strTitle = textInputLayout.getEditText().getText();

然后你可以像这样使用上面的字符串

Toast.makeText(YourClassName.this, strTitle, Toast.LENGTH_SHORT).show();

0
投票

第一个活动:

String uname = userV.getEditText().getText().toString();
    
Intent intent1 = new Intent(VolunteerActivity.this, VolunteerView.class);
intent1.putExtra("USER_NAME", uname);
startActivity(intent1);

第二个活动:

Bundle extras = getIntent().getExtras();
if (extras != null) 
{
    String uname = extras.getString("USER_NAME");
    tv2.setText(uname);
}

0
投票

Kotlin 中,我在从 TextInputEditText 获取文本时遇到了同样的问题,这是我应用的解决方案:

private lateinit var emailForAuthentication : TextInputEditText
private lateinit var passwordForAuthentication : TextInputEditText

emailForAuthentication = findViewById(R.id.emailedittext)

val email = emailForAuthentication.editableText
val password = passwordForAuthentication.editableText

Log.d("login","$email")

说明: 通过将 Editable Type 变量包装在字符串中,我可以展示来自 TextInputEditText 的文本,而不会获得空字符串。

注意: 我没有像之前的答案那样从 TextInputLayout 获取参考,但我正在使用 TextInputEditText 来获取参考。

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        style="?attr/textInputFilledStyle"
        app:endIconDrawable="@drawable/mailicon"
        app:endIconContentDescription="mail icon"
        app:endIconMode="custom"
        android:textColorHint="@color/black"
        app:boxCornerRadiusBottomEnd="50dp"
        app:boxCornerRadiusBottomStart="50dp"
        app:boxCornerRadiusTopEnd="50dp"
        app:boxCornerRadiusTopStart="50dp"
        app:boxStrokeColor="@color/black"
        android:layout_margin="@dimen/_3sdp">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/emailedittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:background="@color/white"/>
    </com.google.android.material.textfield.TextInputLayout>

0
投票

要在吐司中获取文本或打印,这是简单的方法

TextInputLayout editext_usr_sign;

editext_usr_sign = (TextInputLayout) findViewById(R.id.usr);

String a = editext_usr_sign.getEditText().getText().toString().trim();

Toast.makeText(Registration.this, a, Toast.LENGTH_SHORT).show();

© www.soinside.com 2019 - 2024. All rights reserved.