可以使用TextInputLayout和EditText做到这一点。
这里是你的XML:
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Label">
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
添加属性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:
希望这将有助于〜