Android - 如何在EditText上添加两个提示信息?

3

我知道可以为EditText设置提示文本。
目前,我只是使用空格来分隔两个提示。然而,正确的提示文本并没有对齐到EditText的右侧。

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

我需要的是以下样式: enter image description here 如何在单个EditText中左右分别添加提示信息?

给带有 [M/F]EditText 设置 PaddingRight,其余部分与获取方式完全相同。 - Tushar Gogna
这是今天一个非常好的问题。 - IntelliJ Amiya
2个回答

2

你好,你的问题非常有趣。在EditText的源代码中,你可以看到提示只是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"/>

此外,你可以创建自己的属性,使你的视图更加友好易用。

我已经尝试了你的代码,但是右边提示的背景为空,所以它比左边提示低一点。有什么解决办法吗? - Season
让我检查一下,给我几秒钟,我会更新我的帖子。 - Konrad Krakowiak
我很高兴能够帮助你。祝你好运。 - Konrad Krakowiak

0

我认为通过正常手段是不可能的。尝试使用自定义视图,其布局如下:

<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。

我知道这可能是一种解决方案,但它会使代码变得复杂。我希望有一个单独的类可以实现这样的风格。 - Season

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接