虽然EditText使用起来很简单,但是关于EditText还是有不少的注意事项的,今天就来总结一下。
一、Google提供的EditText的边框可能在一些APP的整体布局中显得丑,此时我们需要去掉默认的边框。实现:
android:background="@null" 或者 editText.setBackground(null);
实测,两种方式还是有些微差别的,一般使用
android:background="@null"
即可。
二、显示光标/不显示光标(默认是显示光标的)
通过cursorVisible这个属性。
显示光标
android:cursorVisible="true"
或者
editText.setCursorVisible(true);
不显示光标
android:cursorVisible="false"
或者
editText.setCursorVisible(false);
三、有时候,我们希望光标的颜色与EditText中输入的文本的颜色保持一致。实现:
android:textCursorDrawable="@null"
四、如果我们想指定光标的颜色和宽度呢?我们可以这样来实现:
创建一个shape文件test.xml,比如:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#1985E3" />
<size android:width="1dp"/>
</shape>
然后,一行代码设置
android:textCursorDrawable="@drawable/test"
这样就OK了。
五、给EditText指定我们想要的边框。实现:
创建一个xml文件test2.xml,比如:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#EFEFEF"/>
<corners
android:radius="3dp"
/>
<stroke
android:width="0.5px"
android:color="#505050"/>
</shape>
</item>
</layer-list>
然后,设置background,
android:background="@drawable/test2"
这样就OK了。