我试图做相反的场景 - 提示出现在左侧,键入的文本出现在中间,我在这里找到了我需要的部分解决方案,但为了其他任何人这样做,我想我会发布我的完整解决方案,适应上述场景:)。
因此,当我试图弄清楚这一点时,我发现存在两个挑战:1)要使提示居中显示,而用户键入的任何文本都是左对齐的。2) 让光标立即出现在用户文本将出现的位置,而不是提示开始的位置。我还希望提示一直保留到用户开始输入,并在用户删除他们输入的内容时重新出现。
我的解决方案涉及两个相互叠加的 EditText 框,上面的框包含提示,下面的框包含文本。像这样:
<!-- This EditText box is where the user writes their text.
It is aligned left, doesn't have a hint in it,
and is the desired width of the box
!-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
android:fontFamily="sans-serif-medium"
android:background="@color/white"
android:layout_marginTop="5dp"
android:id="@+id/event_heading"
android:hint=""
android:layout_below="@id/page_title"
/>
<!-- This second EditText box only contains the hint text.
It is centered, is only the width of the hint text,
and for my purposes, the text was italic !-->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textShortMessage"
android:fontFamily="sans-serif-medium"
android:background="@color/white"
android:layout_marginTop="5dp"
android:layout_below="@id/page_title"
android:id="@+id/event_headingHint"
android:hint="Heading"
android:gravity="center"
android:textStyle="italic"
/>
包含提示的框是第二个,所以它出现在另一个的上方。我在片段中使用它,因此我在片段中覆盖 onCreateView,拉出两个 EditText 框,并将它们发送到我编写的用于添加必要侦听器的方法,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_create_event, container, false);
EditText heading = (EditText) view.findViewById(R.id.event_heading);
EditText headingHint = (EditText) view.findViewById(R.id.event_headingHint);
addFormatTidier(heading, headingHint);
return view;
}
否则,如果尝试在 Activity 而不是片段中执行此操作,则可以在 onCreate 方法中使用相同的代码,就在 setContentView 命令之后:
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
setContentView(R.layout.activity_add_event);
EditText heading = (EditText) findViewById(R.id.event_heading);
EditText headingHint = (EditText) findViewById(R.id.event_headingHint);
addFormatTidier(heading, headingHint);
...
}
最后,对于我的“addFormatTidier”方法,我为每个 EditText 框添加了一个侦听器:首先,我将一个 onFocusChange 侦听器附加到“hint”EditText,以便在用户单击提示时将焦点移到另一个 EditText。然后我将 addTextChangedListener 附加到我的另一个 EditText,以便在用户开始输入后删除提示,并在用户删除他们输入的所有内容时恢复提示,如下所示:
private void addFormatTidier(final EditText text, final EditText hint)
{
// save the hint so I can restore it later
final CharSequence hintText = hint.getHint();
// Add a listener to shift focus away from the hint text to the other EditText when the hint is clicked.
hint.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
text.requestFocus();
}
}
});
// Add a listener to remove the hint text when the user starts typing, and to restore it when the user clears what they have typed.
text.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0)
{
hint.setHint("");
}
else
{
hint.setHint(hintText);
}
}
});
}
为了使提示在用户单击该字段后立即消失,如果他们单击离开而不在其中留下任何文本,则提示重新出现,可以使用以下方法代替:
private void addFormatTidier(final EditText text, final EditText hint)
{
// save the hint so I can restore it later
final CharSequence hintText = hint.getHint();
// Add a listener to shift focus away from the hint text to the other EditText when the hint is clicked, and simultaneously clear the hint text.
hint.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
text.requestFocus();
hint.setHint("");
}
}
});
// Add a listener to remove the hint text when the main EditText receives focus, and to restore it when the EditText loses focus without any text inside it.
text.setOnFocusChangeListener(new android.view.View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
hint.setHint("");
}
else
{
if(!(text.getText().length()>0))
{
hint.setHint(hintText);
}
}
}
});
}
我希望这对其他试图弄清楚如何解决类似问题的人有用:D。上面评论中的 userM1433372 让我走上了从哪里开始的正确轨道:D。