【问题标题】:How to display hint text in two sides of editText?如何在editText的两侧显示提示文本?
【发布时间】:2018-10-15 07:24:19
【问题描述】:

你可以看到'email'和'example@abc.com'在EditText的两端。首先,我想使用提示来解决它,如下所示:

     android:hint=" email           example@abc.com"   

但我认为这是一个不好的方法。有什么好的方法可以解决这个问题。

【问题讨论】:

  • 使用简单的提示,我认为这是不可能的。将您的编辑文本包装在框架布局中,对于右侧提示,请使用 textview 并将 textwatcher 添加到您的编辑文本中。然后,一旦用户开始输入,将该文本视图的可见性设置为消失
  • 你好 Vivek Mishra,你解决了我的问题。

标签: android android-layout android-edittext


【解决方案1】:

如何在editText的两侧显示提示文字?

AFAIK 不是 editText 的内置行为,您可以使用单个 editText 实现所需的输出

解决方案

获取LinearLayout 并将EditTextTextView 包裹在LinearLayout

示例代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email" />

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="end"
            android:text="nilesh@mail.com" />

    </LinearLayout>
</LinearLayout>

输出

【讨论】:

  • 使用您的方式,“电子邮件”始终可见。但我希望它在编辑文本时不可见。
  • @PigChou 当你的editext是textview上的空文本提示时,你需要将TextWatcher添加到你的edittext
  • 非常感谢,Nilesh Rathod。
猜你喜欢
  • 2013-06-29
  • 2017-07-27
  • 1970-01-01
  • 2012-01-23
  • 2011-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多