如何在EditText中间设置提示文本,在EditText左侧设置光标?当我开始输入文本时,它是从中心 EditText 开始的吗?
现在我有
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@drawable/line_white"
android:layout_marginBottom="25dp"
android:paddingBottom="16dp"
android:gravity="center_horizontal">
<EditText
android:id="@+id/loginEmailField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:ems="10"
android:hint="Choose an email adress"
android:inputType="textEmailAddress"
android:paddingLeft="2dp"
android:singleLine="false"
android:textColor="#ffffff"
android:textColorHint="#9377ab"
android:textSize="19sp"
android:textStyle="bold"
android:background="#00000000"
android:layout_marginRight="2dp"
android:gravity="center"/>
</LinearLayout>
最佳答案
这是一个有点愚蠢的问题,我知道.. 解决这个问题的最简单方法(并保持文本居中)是使用这个简单的技巧:
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
if (s != null && s.toString().length() > 0){
editText.setGravity(Gravity.CENTER);
}else{
editText.setGravity(Gravity.START);
}
}
});
关于android - 如何使android edittext提示中心和左光标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31780025/