【问题标题】:How to move the cursor without moving the hint in material edittext?如何在不移动材料编辑文本中的提示的情况下移动光标?
【发布时间】:2017-01-26 14:22:45
【问题描述】:

我有这样一个材质字段image,但是我需要像image这样替换光标而不替换提示

<android.support.design.widget.TextInputLayout
        android:id="@+id/layout_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/colorTextGray"
        app:passwordToggleEnabled = "true"
        app:passwordToggleTint="@color/color_ic_visibility"
        app:passwordToggleDrawable="@drawable/ic_visibility">
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp"
            android:backgroundTint="@color/colorNotSelected"
            android:hint="Пароль"
            android:inputType="textPassword"
            android:textColor="@color/colorTextGray"
            android:fontFamily="sans-serif" />
    </android.support.design.widget.TextInputLayout>

【问题讨论】:

  • 您是否尝试将 android:paddingStart 添加到 editText?
  • 是的。但是在那之后浮动标签也移动了。我只需要替换光标

标签: android android-edittext cursor-position


【解决方案1】:

您可以使用 Java 将光标移动到所需位置。在你的Activity 你可以试试这个方法:

EditText editText = (EditText)findViewById(R.id.password);
editText.setSelection(//position);

https://developer.android.com/reference/android/text/Selection.html#setSelection(android.text.Spannable, int)

但是,这仅使用提示是行不通的,因为EditText 需要内容。您可以通过使用以下代码创建自己的提示来解决此问题:

EditText editText = (EditText)findViewById(R.id.password);
editText.setText("Пароль");
editText.setSelection(3); // for example 3

参考:How to set focus to the text after the hint in EditText?

【讨论】:

  • 我的编辑文本为空。我需要覆盖开始光标位置。添加填充后浮动标签也在移动。
  • 您不能仅通过使用提示来实现这一点。您可以使用更新的答案来解决此问题。
  • 没有。我需要在不输入任何文本的情况下移动。文本应该从 40dp 的开头开始
  • 然后使用android:paddingLeft="10dp"
  • 但是在浮动标签移动之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-08
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
相关资源
最近更新 更多