Android-如何在EditText上做2个提示?
我知道我可以为EditText设置提示文本Android-如何在EditText上做2个提示?,android,xml,android-edittext,textview,hint,Android,Xml,Android Edittext,Textview,Hint,我知道我可以为EditText设置提示文本 目前,我只使用空格分隔两个提示。但是,右侧提示文本不与EditText右侧对齐 <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:hint="LEFT RIGHT" /
目前,我只使用空格分隔两个提示。但是,右侧提示文本不与EditText右侧对齐
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:hint="LEFT RIGHT" />
我想要的是以下风格:
一个编辑文本怎么能在左边提示一个,在右边提示一个呢?我认为用正常的方法是不可能的。尝试使用具有如下布局的自定义视图:
<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添加到主保持架中,并在内部设置要查看的提示保持架的可见性。在EditText的源代码中,您可以看到提示只是CharSequence。无法设置双重提示。但是您可以创建自己的视图来实现这一点。看看我的解决方案。这很简单,而且很有效 两个提示\u编辑\u 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>
在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"/>
此外,您还可以创建自己的属性,以使视图更易于使用。使用[M/F]将
PaddingRight
赋予编辑文本
,rest正是您获得它的方法。这是一个非常好的问题,我知道这将是一种解决方案。但它使代码变得复杂。我希望有一个类可以创建这样的样式。我已经尝试了您的代码,但右提示比左提示低一点,因为它的背景为空。有什么解决办法吗?让我检查一下,给我几秒钟,我会更新我的帖子。我很高兴我能帮助你。祝你好运
<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"/>