EditText 中缺少光标和下划线
Cursor and Underline missing in EditText
我在 Toolbar
中使用 EditText
,无论我做什么,光标和下划线都不会出现
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>
科特林代码
editText?.visibility = View.VISIBLE
editText?.requestFocus()
editText?.isCursorVisible = true
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
我哪里错了,有人能帮忙吗?
这是因为默认EditText的下划线和光标是colorAccent
。因此,如果您想将它们的颜色更改为白色,那么您应该在 styles.xml
中定义一个样式,如下所示:
<style name="editText" parent="AppTheme">
<item name="colorAccent">@color/white</item>
</style>
然后将此样式作为主题应用到 edittext,如下所示:
<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"
/>
- 定义 4 XML 个文件:
edit_text_cursor.xml
自定义的矩形可绘制对象,可更改 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>
edit_text_underline.xml
EditText 状态未聚焦时的下划线形状
<?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>
edit_text_underline_focused.xml
EditText 状态聚焦时的下划线形状
<?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 状态(聚焦与否)显示不同的形状 drawable。
<?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>
- 将 EditText 设置更改为 blew:
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>
我在 Toolbar
中使用 EditText
,无论我做什么,光标和下划线都不会出现
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>
科特林代码
editText?.visibility = View.VISIBLE
editText?.requestFocus()
editText?.isCursorVisible = true
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
我哪里错了,有人能帮忙吗?
这是因为默认EditText的下划线和光标是colorAccent
。因此,如果您想将它们的颜色更改为白色,那么您应该在 styles.xml
中定义一个样式,如下所示:
<style name="editText" parent="AppTheme">
<item name="colorAccent">@color/white</item>
</style>
然后将此样式作为主题应用到 edittext,如下所示:
<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"
/>
- 定义 4 XML 个文件:
edit_text_cursor.xml
自定义的矩形可绘制对象,可更改 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>
edit_text_underline.xml
EditText 状态未聚焦时的下划线形状
<?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>
edit_text_underline_focused.xml
EditText 状态聚焦时的下划线形状
<?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 状态(聚焦与否)显示不同的形状 drawable。
<?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>
- 将 EditText 设置更改为 blew:
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>