问题
I have an EditText with some information in it. When there is no information,
I'm displaying a caption using setHint().
final EditText info = new EditText(activity);
if (data.getinfo() != null && !data.getAddress().trim().equals("")) {
info.setText(data.getinfo());
} else {
info.setHint(R.string.info);
}
I want to show that caption even if the EditText has info, the info should be below the caption.
I tried using setText(). But that didn't work either.
if (condition) {
info.setText(R.string.info);
info.setText(data.getinfo());
} else {
info.setHint(R.string.info);
}
回答1:
Within a RelativeLayout, use a TextView as an overlay View on top of your EditText. Use appropriate colors, size for the font to achieve the effect you are looking for.
Here is a hint
<RelativeLayout …>
<EditText android:id="@+id/name"
android:layoutWidth="match_parent"
android:layoutHeight="wrap_content" >
<TextView android:id="@+id/name_hint"
android:layoutWidth="wrap_content"
android:layoutHeight="wrap_content"
android:gravity="left|center_vertical"
android:layout_alignLeft="@id/name"
android:layout_alignRight="@id/name"
android:layout_alignTop="@id/name"
android:layout_alignBottom="@id/name"
android:layout_alignBottom="@id/name" >
</RelativeLayout>
This is just a starting point, you can adjust the height of your EditText, position your TextView, change its gravity, etc., to achieve what you are looking for.
回答2:
You can directly do it through xml tag which you've added in your UI layout.
来源:https://stackoverflow.com/questions/13289447/edittext-with-a-permanent-hint-in-java