14

嗨,我有一个带有提示的编辑文本,我使用android:gravity:"center". 当我从edittext中开始打字时,打字是从中间开始的,如何让打字从左角开始,同时仍然提示居中

<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" />
4

3 回答 3

25

I don't think this is possible "out-of-the box" but you can do it programmatically:

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);  
          }
     }
}
于 2013-01-22T15:02:06.427 回答
19

默认情况下,android EditText 具有中心重心。做吧android:gravity="top|left"

此外,没有任何特殊属性可用于在中心制作提示和在顶部制作文本。两者都遵循相同的重力属性。

Android提示只是用于用户必须开始输入字符的标签之类的东西。

于 2013-01-22T14:52:42.010 回答
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);
    ...
}

最后,对于我的“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。

于 2015-08-11T20:43:41.033 回答