android - 如何使android edittext提示中心和左光标

标签 android android-edittext

如何在EditText中间设置提示文本,在EditText左侧设置光标?当我开始输入文本时,它是从中心 EditText 开始的吗?

enter image description here

现在我有

enter image description here

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

相关文章:

android - 在 ListView 中保留一个 EditText 值

Android:如果应用程序从通知加载,则不会显示在任务管理器中

php - Android MySQL 服务器请求类中 Asynctask 的问题

android studio error-混合版本会导致运行时崩溃

android - 如何为EditText设置边框颜色

java - 使用图像和环绕图像的文本编辑 TextView

android - 是否可以在 Android 中运行 python libsvm?

java - Android 应用程序中共享首选项声明中出现 NullPointerException?

android - 三星 Galaxy Tab 10.1、虚拟键盘和编辑文本

android - Listview 软键盘中的 EditText 问题