您可以使用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>
1. 将属性android:hint="Label"
添加到TextInputLayout
中,以始终显示其提示Label
。
2. 当EditText
被聚焦时,通过编程方式设置EditText
提示Placeholder
。
在您的Activity中添加以下行:
.........
.................
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.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>
<style name="TextLabel" parent="TextAppearance.AppCompat.Light">
<item name="colorAccent">@color/yourColor</item>
</style>
据我所知,将2个不同的文本(占位符和提示)放在一起解决此任务的最佳方式是使用以下代码:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:hintEnabled="true" // enable hint/placeholder on EditText
android:hint="@string/title_of_input" // title shown above EditText
app:expandedHintEnabled="false" // disable move placeholder to hint place
app:placeholderText="@string/placeholder_for_edittetx" // placeholder shown in EditText when no text is filled by user
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
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>
给定:一个嵌套在TextInputLayout
中的TextInputEditText
!
简而言之:使用这个Kotlin扩展函数。
fun EditText.setHintAndLabel(
textInputLayout: TextInputLayout,
label: String?, // hint in the TextInputLayout
hint: String? // hint in the EditText
) {
this.hint = ""
textInputLayout.hint = label
this.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
this.hint = hint ?: ""
} else {
this.hint = ""
}
}
}
问题是什么,它如何解决?
问题在于,如果TextInputLayout
中有提示,则EditText
的提示会重叠。在这种情况下显示哪个提示呢?好问题:我们只想在EditText
获得焦点/光标在内部时显示其提示,但是TextInputLayout
提示始终应该显示。
⮑ 因此,我们只在EditText
获得焦点时设置提示,并在失去焦点时将其删除。
<?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>
<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");
}
}
});
android:focusableInTouchMode="true"
。 - Ferdous AhamedfocusableInTouchMode="false"
来避免双击问题。当你将focusableInTouchMode
设置为true
时,你告诉 View 它应该 仅 在第一次点击时聚焦视图,而不执行操作(在这种情况下显示键盘),导致用户需要点击两次 View 才能显示键盘。另一个例子是在Button
上设置focusableInTouchMode
为true
,这样做需要两次点击才能调用onClickEvent
- 因为第一次点击仅在focusableInTouchMode="true"
时聚焦视图。 - Sakiboyandroid:id
并在其上设置focusable="true"
,而不是在父/根布局上设置focusableInTouchMode
。 - Sakiboy