Android - 如何在 EditText 上制作 2 个提示?

Android - how to make 2 hints on an EditText?

我知道我可以为 EditText 设置提示文本。
目前,我只使用 space 来分隔 2 个提示。但是,右侧的提示文本没有与 EditText 的右侧对齐。

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:hint="LEFT                RIGHT" />

我想要的是如下样式:

如何为单个 EditText 在左侧创建一个提示,在右侧创建另一个提示?

我认为通过正常方式是不可能的。尝试使用具有如下布局的自定义视图:

<FrameLayout
    android:id="@+id/main_holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/hints_holder"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/left_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left hint" />

        <View
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="0dp" />

        <TextView
            android:id="@+id/right_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left hint" />
    </LinearLayout>
</FrameLayout>

然后只需将 OnClickListener 添加到 main_holder 并将 hints_holder 的可见性设置为 View.GONE

您好,您的问题很有趣。在EditText的源代码中可以看到hint只是CharSequence。无法设置双重提示。但是您可以创建自己的视图来允许这样做。看看我的解决方案。非常简单而且有效。

two_hints_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_gravity="center_vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/non_active_edit_text"
            android:focusable="false"
            android:gravity="right|center_vertical"
            android:hint="Right"
            android:paddingTop="4dp"
            android:background="@null"/>


    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/active_edit_text"
            android:hint="Left"
            android:layout_gravity="center_horizontal"/>
</merge>

EditTextWithTwoHints.java 文件

public class EditTextWithTwoHints extends RelativeLayout{

    EditText activeField;
    EditText nonActiveEditText;

    final CharSequence hint;

    public EditTextWithTwoHints(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.two_hints_edit_text, this);
        activeField = (EditText)findViewById(R.id.active_edit_text);
        nonActiveEditText = (EditText)findViewById(R.id.non_active_edit_text);
        hint = nonActiveEditText.getHint();
        activeField.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                nonActiveEditText.setHint(s.length() !=0 ? "" : hint);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

xml

中的使用示例
<you.package.EditTextWithTwoHints
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

此外,您可以创建自己的属性,使您的视图更易于使用。