2012-10-22 60 views
1

嗨,我正在与包含登录屏幕的android应用程序。在我的登录屏幕中,我有用户名和密码edittext字段。在edittext中,我需要将提示作为“输入用户名”放在中心位置,所以我将重力设置为edittext字段的“center”,但现在问题是光标在中心位置也可见,我需要光标位于左侧。我也尝试了下面的编码。光标在android的中心位置

edtUserName.setSelection(1); 

This is my xml code: 
<EditText 
       android:id="@+id/edt_login_username" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/bg_txt_field" 
       android:ellipsize="start" 
       android:focusableInTouchMode="true" 
       android:hint="Enter Username" 
       android:maxLines="1" 
       android:singleLine="true" 
       android:textStyle="italic" 
       android:typeface="serif" android:gravity="center"> 
      </EditText> 

Please help me to solve this problem. Thanks in advance. 
+0

提供XML文件Plz .. – Bhavin

+0

您好我现在添加了xml代码,请刷新并找到代码 – Dhamodharan

+0

您是否解决了这个问题? – Salmaan

回答

0

我想这会帮助你。

android:引力设置引力内容的视图使用。 android:layout_gravity在视图或布局的父级中设置重力。 和示例:Example

0

改变重力:

android:gravity="center_vertical" 
+0

不适用于我的情况 – Dhamodharan

1

你可以试试这个

<EditText 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/editText" 
android:textAlignment="center" 
android:ellipsize="end" 
android:gravity="center"/> 
2

您可以通过编程实现它!

yourEditText.addTextChangedListener(新TextWatcher(){ 公共无效afterTextChanged(编辑S){} 公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,诠释后){}

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); 
     } 
} 

}