EditText中缺少光标和下划线

时间:2018-05-31 08:07:24

标签: android kotlin android-edittext android-toolbar

我在EditText内使用Toolbar,无论我做什么,光标和下划线都不会出现

xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    app:collapseIcon="@drawable/ic_arrow_up_24dp"
    app:titleTextColor="@color/white"
    android:layout_alignParentTop="true"
    android:animateLayoutChanges="true"
    android:visibility="gone"
    android:alpha="0.0"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:cursorVisible="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_height="wrap_content"
        android:hint="Search..."
        android:textCursorDrawable="@color/white"
        android:textColor="@color/md_white_1000"
        android:layout_marginRight="120dp"
        />

</LinearLayout>
</android.support.v7.widget.Toolbar>

kotlin代码

        editText?.visibility = View.VISIBLE
        editText?.requestFocus()
        editText?.isCursorVisible = true
        editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
        editText.setTextIsSelectable(true);

我哪里出错了,有人可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

这是因为默认情况下,EditText的下划线和光标为colorAccent。所以,如果你想改变它们的颜色以假设白色,那么你应该在styles.xml中定义一个样式,如下所示:

<style name="editText" parent="AppTheme">
    <item name="colorAccent">@color/white</item>
</style>

然后将此样式作为主题应用于此类编辑文本:

 <EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:cursorVisible="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:layout_height="wrap_content"
    android:hint="Search..."
    android:textCursorDrawable="@color/white"
    android:textColor="@color/md_white_1000"
    android:theme="@style/editText"
    android:layout_marginRight="120dp"
    />

答案 1 :(得分:1)

  1. 定义4个XML文件:
  2. edit_text_cursor.xml一个自定义的矩形形状drawable,用于更改EditText的光标。

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@color/white"/>
            <size android:width="2dp"/>
    </shape>
    
    当EditText状态未聚焦时

    edit_text_underline.xml下划线

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="0dp"
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape>
                <solid android:color="@android:color/transparent" />
                <stroke
                    android:width="2dp"
                    android:color="@android:color/darker_gray" />
                <padding android:bottom="4dp" />
            </shape>
        </item>
    </layer-list>
    
    当EditText状态为

    时,

    edit_text_underline_focused.xml下划线形状

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:bottom="0dp"
            android:left="-2dp"
            android:right="-2dp"
            android:top="-2dp">
            <shape>
                <solid android:color="@android:color/transparent" />
                <stroke
                    android:width="2dp"
                    android:color="@android:color/white" />
                <padding android:bottom="4dp" />
            </shape>
        </item>
    </layer-list>
    

    edit_text_underline_selector.xml根据EditText状态显示不同的可绘制形状(聚焦与否)。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/edit_text_underline_focused" android:state_focused="true"/>
        <item android:drawable="@drawable/edit_text_underline" android:state_focused="false"/>
    </selector>
    
    1. 将EditText设置更改为blew:
    2. xml代码

      <android.support.v7.widget.Toolbar
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toolbar"
          app:collapseIcon="@drawable/ic_arrow_up_24dp"
          app:titleTextColor="@color/white"
          android:layout_alignParentTop="true"
          android:animateLayoutChanges="true"
          android:visibility="gone"
          android:alpha="0.0"
          android:layout_width="match_parent"
          android:layout_height="?attr/actionBarSize"
          android:background="@color/colorPrimary">
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              android:gravity="center_vertical">
              <EditText
                  android:id="@+id/edit_text"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:cursorVisible="true"
                  android:focusable="true"
                  android:focusableInTouchMode="true"
                  android:hint="Search..."
                  android:textColor="@color/white"
                  android:background="@drawable/edit_text_underline_selector"
                  android:textCursorDrawable="@drawable/edit_text_cursor"/>
          </LinearLayout>
      </android.support.v7.widget.Toolbar>