嗨我有一个带提示的编辑文本,我使用android:gravity:"center"
将提示设为中间。当我从edittext开始输入时,键入从中间开始,如何使键入从leftcorner开始,同时仍然提示中心
<EditText
android:id="@+id/edittext"
android:layout_width="230dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="5dp"
android:layout_marginTop="33dp"
android:layout_toLeftOf="@+id/relativeLayout1"
android:background="@drawable/roundcorners"
android:ems="10"
android:gravity="center_horizontal"
android:hint="@string/fbhint"
android:lines="6"
android:paddingRight="5dp"
android:textSize="14sp"
android:windowSoftInputMode="stateHidden" />
答案 0 :(得分:24)
我认为这不是“开箱即用”的可能,但您可以通过编程方式进行:
yourEditText.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){
// position the text type in the left top corner
yourEditText.setGravity(Gravity.LEFT | Gravity.TOP);
}else{
// no text entered. Center the hint text.
yourEditText.setGravity(Gravity.CENTER);
}
}
}
答案 1 :(得分:15)
默认情况下,android EditText具有中心重力。只需将其android:gravity="top|left"
此外,没有任何特殊属性可以在中心和文本中进行提示。两者都遵循相同的引力属性。
Android提示仅适用于标签,用户必须开始输入字符。
答案 2 :(得分:1)
我试图做相反的情况 - 因为提示出现在左边,输入的文字显示在中间,我找到了我需要的部分解决方案,但是为了其他任何人的利益我试图这样做,我想我会发布完整的解决方案,适应上述情况:)。
因此,当我试图解决这个问题时,我发现有两个挑战: 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);
...
}
然后最终为我的&#39; addFormatTidier&#39;方法,我为每个EditText框添加了一个监听器:首先,我将一个onFocusChange监听器附加到&#39;提示&#39; 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。