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. 带有占位符的Regexp_like
- 4. Android - 更改TextInputLayout的位置浮动标签(底部浮动标签)?
- 5. Android标签和不同的活动
- 6. 去掉占位符内的xml标签
- 7. XtraReport的占位符类型标签
- 8. 带有复选框的Textarea占位符
- 9. Codenameone带有占位符的FontImage
- 10. Tensorflow,带有Estimator(model_fn)的占位符?
- 11. 不能嵌套占位符捕获相同的占位符'
- 12. 使用HTML5占位符等标签
- 13. 占位符VS标签用于HTML5
- 14. 隐藏的标签,但显示占位符在同一时间?
- 15. 带有图标/按钮的Android EditText
- 16. 如何设置edittext的浮动标签文本颜色,当它不是焦点不同于edittext提示颜色?
- 17. Android HTML占位符
- 18. Python dictonary带有字符串占位符和默认值
- 19. Spring MVC jsp标签占位符标签值
- 20. 如何创建一个带有占位符动画的表单?
- 21. UITextView标签中的占位符不会消失
- 22. 带有占位符和外键的Perl DBI MySQL插入
- 23. 如何从UISearchBar移动占位符标签
- 24. AngularJs:基于值的不同占位符
- 25. 不同颜色的占位符
- 26. 动态更改标签和占位符取决于加载的html
- 27. 占位符不是在目标Maven的
- 28. 你如何制作占位符和标签转换?
- 29. 带有深色文字的占位符(不晕)
- 30. 在所有标签上应用占位符?
有没有一种方法,以保持标签和占位符像对焦状态的截图? –
是可能的。使用'android:focusableInTouchMode =“true”'为父/容器布局。 – FAT