具有不同浮动标签和占位符的Android EditText [英] Android EditText with different floating label and placeholder

查看:169
本文介绍了具有不同浮动标签和占位符的Android EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建如下所示的editText?

How can i create an editText that looks like this?

推荐答案

您可以使用TextInputLayoutEditText.

这是您的XML:

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Label">

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

</android.support.design.widget.TextInputLayout>

1..将属性android:hint="Label"添加到TextInputLayout以始终显示其提示Label.

1. Add attribute android:hint="Label" to TextInputLayout to show its hints Label always.

2..只有当EditText变得专注时,才能以编程方式设置EditText提示Placeholder.

2. Programmatically set EditText hints Placeholder only when EditText get focused.

在活动"中添加以下行:

    .........
    .................

    final EditText editText = (EditText) findViewById(R.id.edit_text);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                editText.setHint("Placeholder");
            } else {
                editText.setHint("");
            }
        }
    });

    .........
    ..................

输出:

希望这会有所帮助〜

这篇关于具有不同浮动标签和占位符的Android EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆