5
A
回答
0
您可以如下使用以下布局的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:textColor="@color/wallet_holo_blue_light" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText2"
android:hint="Placeholder" />
</LinearLayout>
2
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
<android.support.design.widget.TextInputEditText
android:hint="Placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
注意android:hint="Placeholder"
从TextInputEditText
在同一时间android:hint="Label"
从TextInputLayout
时可见视图不集中。你可以在你的java代码中做一些额外的检查来显示和隐藏标签。或者只需从TextInputLayout
开始android:hint="Placeholder"
。
要更改颜色,您需要使用android:theme="@style/TextLabel
设置TextInputLayout
的主题,并设置颜色重音。
<style name="TextLabel" parent="TextAppearance.AppCompat.Light">
<item name="colorAccent">@color/yourColor</item>
</style>
0
如果您在edittext的焦点上使用textinputlayout,那么您没有得到任何占位符。
布局:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/username_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
你要设置的EditText的焦点变化监听器。
的Java:
usernameTxt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
usernameTxt.setHint("Label");
} else {
usernameTxt.setHint("Placeholder");
}
}
});
6
可以使用TextInputLayout
和EditText
做到这一点。
这里是你的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>
添加属性android:hint="Label"
到TextInputLayout
显示其提示Label
始终。
2.编程设置EditText
提示Placeholder
只有当EditText
得到关注。
添加以下线在你的活动:
.........
.................
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("");
}
}
});
.........
..................
OUTPUT:
希望这将有助于〜
2
您可以使用下面的代码(在科特林)。它将在延迟200毫秒后显示占位符(以避免重叠提示和占位符)。
class PlaceholderEditText : TextInputEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val placeholder = hint
init {
hint = ""
onFocusChangeListener = OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
postDelayed({ hint = placeholder }, 200)
} else {
hint = ""
}
}
}
}
,然后在布局XML类:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="ALWAYS VISIBLE LABEL">
<com.myapp.widget.PlaceholderEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DISAPPEARING PLACEHOLDER" />
</android.support.design.widget.TextInputLayout>
相关问题
- 1. 浮动占位符
- 2. EditText浮动标签重力
- 3. Android - 更改TextInputLayout的位置浮动标签(底部浮动标签)?
- 4. 去掉占位符内的xml标签
- 5. Tensorflow,带有Estimator(model_fn)的占位符?
- 6. 不能嵌套占位符捕获相同的占位符'
- 7. 占位符VS标签用于HTML5
- 8. 使用HTML5占位符等标签
- 9. 隐藏的标签,但显示占位符在同一时间?
- 10. Android HTML占位符
- 11. Spring MVC jsp标签占位符标签值
- 12. 如何创建一个带有占位符动画的表单?
- 13. UITextView标签中的占位符不会消失
- 14. 不同颜色的占位符
- 15. 如何从UISearchBar移动占位符标签
- 16. 占位符不是在目标Maven的
- 17. 动态更改标签和占位符取决于加载的html
- 18. 带有深色文字的占位符(不晕)
- 19. 你如何制作占位符和标签转换?
- 20. 在所有标签上应用占位符?
- 21. 如何在OnFocusChangeListeners更新UI线程之前让EditText浮动标签浮动?
- 22. PHP替换字符串标签/占位符/标记与动态值
- 23. 造型MD-无浮占位符
- 24. 不同的标签图标和主要活动在android 7
- 25. pymssql和占位符
- 26. 带有文本占位符的Phonegap和Jquery移动输入号码
- 27. 如何在wordpress comments.php中获取占位符而不是标签?
- 28. asp标签在asp.net内容占位符中不被识别
- 29. 的Android的EditText怪异浮动光标下的白盒
- 30. 带有占位符右对齐的UITextField不能正确地向右移动
有没有一种方法,以保持标签和占位符像对焦状态的截图? –
是可能的。使用'android:focusableInTouchMode =“true”'为父/容器布局。 – FAT