编辑框提示 - 始终显示提示信息

3
我有一个带提示的文本框,但我希望提示始终显示,即使TB中有输入。Gmail应用程序中的示例是“至”字段。

1
我认为那里没有提示,你可以看到光标只在“To”之后开始。但是,如果你同意这种方法,你可以使用LinearLayout来模仿它,我会写一个完整的答案。 - Ron.B.I
听起来不错,你介意吗? - Jonny Wright
1
没问题 - 我已经添加了一个答案,我建议您使用第三个选项,如果有任何问题请告诉我。 - Ron.B.I
使用 setOnFocusChangeListener 来实现该功能。 - Atif AbbAsi
2个回答

2

有三种方法可以使用(提示:使用第3种方法),因为如我在评论中提到的,在Gmail示例中,它不是实际的提示:

  1. Using a Linear Layout, getting a cleaner look in my opinion:

    <LinearLayout android:layout_width="fill_parent"
              android:layout_height="wrap_content">
    <TextView android:text="@string/Hint"
              android:layout_margin="5dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
    <EditText android:layout_width="fill_parent"
              android:inputType="text"
              android:layout_height="wrap_content"/>    
    </LinearLayout>
    
  2. Using a Relative Layout, getting a result that mimics the Gmail App:

    Note: might be problematic since the text will be displayed on top of the hint, see solution 3.

    <RelativeLayout android:layout_marginTop="10dp"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content">
        <TextView android:text="@string/Hint"
              android:paddingLeft="10dp"
              android:layout_marginBottom="0dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"/>
        <EditText android:layout_width="fill_parent"
              android:inputType="text"
              android:layout_height="wrap_content"/>    
    

    Result are as shown in this image:

    Custom Permanent Hint

    Edit:

  3. using a Drawable:

    This seems a better solution (I personally just created it from snipping the 1:1 display of the TextView, will be in correct measurements this way), this will allow a cleaner layout code, and the text will be aware of the drawable:

    <RelativeLayout android:layout_marginTop="10dp"
         android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              >
    <EditText android:layout_width="fill_parent"
              android:inputType="text"
              android:gravity="top"
              android:drawableLeft="@drawable/Hint"
              android:layout_height="wrap_content"/>    
    

    Drawable Hint


0

我脑海中首先想到的是在RelativeLayout中插入两个editText布局。 第一个在第二个上方。在顶部设置背景为 android.R.color.transparent 操作相对布局的顶部元素!在下面的项目中-设置提示(你想要的)并尝试输入文本。可能需要指定顶部第一个元素的填充来获得更好的显示效果。

哦,还有一件事。 也许将editText背景设置为背景图像-但不易于可扩展性。


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