在使用EditText的XML 文件中加入一个属性:
android:textCursorDrawable="@null"
android:textCursorDrawable 这个属性是用来控制光标颜色的,
"@null" 是作用是让光标颜色和text color一样
如果需要自定义颜色,需要自定义一个drawable文件,例如:在drawable下窗井my_cursor.xml,内容如下
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#000080" /> <size android:width="1dp"/> </shape>
然后,设置android:textCursorDrawable="@drawable/my_cursor",光标颜色就可以改变为指定颜色。
注:不能直接设置颜色,因为颜色没有宽度
在项目中,有时候想要编辑框开始不获取焦点,当点击编辑框获得焦点时,会调到下一个Activity中。
一般情况下,一进入一个页面, EditText默认就会自动获取焦点。
解决之道:在EditText的父级控件中找一个,添加这两个属性:
这样,就把EditText默认的行为截断了!
EditText设置可以编辑和不可编辑状态
用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);设置不可编辑状态;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();设置可编辑状态
这个可以实现可编辑和不可编辑,但是又发现一个问题,在不可编辑状态如果常按住控件,可以进行粘帖操作,不知道怎么可以不能进行粘帖啊
EditText属性(API 24)
- android:hint //设置hint提示文本
- android:textColorHint //设置hint提示文字颜色
- android:textColor //设置文字颜色
- android:textSize //设置文本字体大小
- android:textStyle //设置文本字体样式,bold(加粗),italic(倾斜),normal(默认是正常字体).
- android:numeric //控制EditText输入数字的类型 decimal(浮点数),integer(正整数),signed(带+ -号的整数).注意:该属性在使用时提示已过时,建议使用android:inputType属性替代.
- android:singleLine //设置是否单行输入 true(单行输入),false(会自动换行).注意:盖属性在使用时提示已过时,建议使用android:maxLines="1"属性替代.
- android:password="true" //以密文的形式显示输入的文本.注意:该属性在使用时提示已过时,建议使用android:inputType属性替代.
- android:textAlignment //设置EditText中文本显示的位置,center(居中),inherit(默认,居左边显示),viewStart(居左显示),viewEnd(居右显示),textStart(居左显示),textEnd(居右显示).这里需要注意的是最低支持的API版本是17,前两个可以在API14中使用,而后面使用就会报红线...
- android:textColorHighlight //设置被选中字体的颜色.默认为 Theme 主题中的 “colorAccent”的颜色.
- textCursorDrawable //设置被光标的颜色.默认为 Theme 主题中的 “colorAccent”的颜色.
- android:textScaleX //设置文本的水平缩放系数.
- android:typeface //设置hint提示文本的字体.normal(默认),monospace,sans,serif.这里就不解释了,大家试一下就能看出效果.
- android:background //设置EditText背景."@null"设置背景为透明.当我们设置背景后,EditText的那条线就会消失.
- android:textAppearance //设置文本的颜色,字体,大小和样式.
- android:digits //设置只接收指定的文本内容.
- android:phoneNumber="true" //设置输入电话号码.注意:该属性在使用时提示已过时,建议使用android:inputType属性替代.
- android:editable //设置EditText是否可以编辑.当你设置为true,会提示你该EditText已经是可编辑的.设置为false时会提示使用inputType替代.
- android:inputType //设置文本的类型,用于帮助输入法显示合适的键盘类型.
- android:maxLength //设置EditText最多接受的文本的个数.
- android:lines //设置EditText显示的行数,设置两行就显示两行,即使第二行没有数据.
- android:lineSpacingExtra //设置行间距.
- android:lineSpacingMultiplier //设置行间距的倍数. 如设置成1.5倍.
- android:imeOptions //设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号.下面会详细说明.
使用细节
我们在使用EditText中有时候会限制输入框中输入的文本类型,或者当弹出软键盘时,出现的是比较合适的输入法.如:我们在扣扣时,弹出的软件盘显示的就是数字,当输入密码时,右下角编辑框显示的是"完成",点击即会关闭软键盘.其实这也就是inputType和imeOptions属性来实现的,inputType属性可以指定键盘的类型,而imeOptions指定键盘右下角显示的Action.下面我们就来实现EditText使用过程中的小细节.
-
这个类似QQ登录输入框,第一个EditText(用户名)的inputType设置的是text,imeOptions设置的是actionNext(下一个).第二个EditText(密码)的inputType设置的是textPassword,imeOptions设置的是actionDone(完成).当输入完用户名,点击键盘action下一个,会跳到密码输入框,当输入完密码后,点击键盘action完成,软键盘就会隐藏.来看下效果图就明白来.
效果图:
Xml布局:
<EditText android:id="@+id/et_user_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名..." android:imeOptions="actionNext" android:inputType="text" android:textColor="@color/black" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码..." android:imeOptions="actionDone" android:inputType="textPassword" android:textColor="@color/black" />
注意:这里需要注意的是,如果想让键盘显示Action,需要inputType和imeOptions结合使用才可以,只使用imeOptions是不会有效果的.只会显示默认的换行action.(不同手机的输入法不一样,可能显示的会有点小差别)
-
接下来我们看一下inputType可以接受的参数:
我们可以使用android:inputType属性指定要用于EditText对象的键盘类型.例如,如果你希望用户输入电子邮件地址,则应使用textEmailAddress输入类型.以下是输入类型常见的值:
- "text" 普通文本键盘
- "textEmailAddress" 带有@字符的普通文本键盘
- "textUri" 带有/字符的普通文本键盘.
- "number" 基本数字键盘.
- "phone" 电话样式键盘.
- "datetime" 时间日期.
-
"date" 日期.
android:inputType还允许指定某些键盘行为,例如是否大写所有新单词或使用自动完成和拼写建议等功能.以下是定义键盘行为的一些常见输入类型值:
-
"textCapSentences" 普通的文本键盘,大写每个新句子的第一个字母.
- "textCapWords" 大写每个单词的正常文本键盘.适合标题或人名.
- "textAutoCorrect" 正常文本键盘,可纠正拼写错误的字词.
- "textPassword" 这个就和设置password="true"是一样的效果.以原点的形式显示输入的文本.
- "textMultiLine" 普通文本键盘,允许用户输入包含换行符的长字符串(回车符).
-
-
-
上面演示第一个小栗子时,说了当需要指定键盘action时,需要和inputType结合使用才会有效果,下面就来看下imeOptions可以接受的参数:下面直接来张图,大家一看就明白了.上图....
在代码中我们可以响应action操作的事件.如xml布局中有一个id为search的EditText控件,并指定imeOptions="actionSearch",我们可以指定action操作的事件.
EditText editText = (EditText) findViewById(R.id.search); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { Toast.makeText(this, "点击actionSearch执行的操作 ", Toast.LENGTH_SHORT).show(); handled = true; } return handled; } });
注意:再次提醒,在使用imeOptions时,如果你没有使用inputType属性,是不会有效果的.如果你还在使用Eclipse开发,可能还会用到android:singleLine="true"这一属性.return true表示吃掉事件。
-
最后一个就是使用EditText中遇到的坑了....相信大多数开发者都遇到过,那就是当你的EditText输入框在屏幕的下方时,弹出软键盘会遮挡住输入框...当然,你可以使用ScrollView嵌套EditText来解决这一问题.但我最近看到一篇文章,感觉很优雅的就解决了这个问题.下面贴出文章地址,我就不罗嗦了.软键盘挡住输入框问题的终极解决方案.
『软键盘挡住了输入框』这个坑:
1.让输入法把布局顶上去
传统的做法:对该Activity设置adjustResize
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustPan">
...
</activity>
附:adjustPan与adjustResize区别:
adjustPan:不调整布局的高度,布局整体上移,移出屏幕顶部
adjustResize:动态调整布局高度,重新计算弹出软键盘之后的界面大小,相当于是用更少的界面区域去显示内容,以适应由于输入法导致被压缩的屏幕空间。
若存在webView或者全屏模式
这时候,情况就会变得复杂了:
- 首先,页面是
非全屏模式
的情况下,给activity设置adjustPan
会失效。 - 其次,页面是
全屏模式
的情况,adjustPan
跟adjustResize
都会失效。
全屏模式
即是页面是全屏的,包括Application或activity使用了Fullscreen主题、使用了『状态色着色』、『沉浸式状态栏』、『Immersive Mode』等等——总之,基本上只要是App自己接管了状态栏的控制,就会产生这种问题。
下面这个表格可以简单列举了具体的情况。

躲坑姿势
如前文所示,出现坑的条件是:带有WebView的activity使用了全屏模式
或者adjustPan
模式。
那么躲坑的姿势就很简单了——
如果activity中有WebView,就不要使用全屏模式
,并且把它的windowSoftInputMode值设为adjustResize
就好了嘛
AndroidBug5497Workaround
我个人认为最好的解决方案是这个:AndroidBug5497Workaround,只需要一个神奇的AndroidBug5497Workaround
类。
看名字就知道,它是专门用来对付"5497"问题的,使用步骤也是超级简单:
- 把
AndroidBug5497Workaround
类复制到项目中 - 在需要填坑的activity的onCreate方法中添加一句
AndroidBug5497Workaround.assistActivity(this)
即可。
经过测试,基本在各个Android版本上都可用,效果基本与设置了adjustResize
相当。
看一个对比图:

来自我厂App的某个使用WebView的全屏模式
Activity页面,从左到右分别是:没有软键盘的样式、软键盘挡住输入框的效果、以及使用AndroidBug5497Workaround之后的最终效果。
或者:
解决方法就是从写了fitSystemWindows方法,然后将重写的布局作为根布局,再设置fitSystemWindows = true:
- LinearLayout:
/**
* 解决activity设置了状态栏透明,又设置了android:windowSoftInputMode="adjustResize"的时候输入框被输入法挡住的情况<br>
* 这个作为layout的根布局(root),并设置<code>android:fitsSystemWindows="true"</code>
* Created by Awen <Awentljs@gmail.com>
*/
public class CustomInsetsLinearLayout extends LinearLayout {
private int[] mInsets = new int[4];
public CustomInsetsLinearLayout(Context context) {
super(context);
}
public CustomInsetsLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomInsetsLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public final int[] getInsets() {
return mInsets;
}
@Override
protected final boolean fitSystemWindows(Rect insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Intentionally do not modify the bottom inset. For some reason,
// if the bottom inset is modified, window resizing stops working.
// TODO: Figure out why.
mInsets[0] = insets.left;
mInsets[1] = insets.top;
mInsets[2] = insets.right;
insets.left = 0;
insets.top = 0;
insets.right = 0;
}
return super.fitSystemWindows(insets);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<com.demo.widget.insets.CustomInsetsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
android:orientation="vertical">
<-- main content,这里是你的主要内容-->
</com.demo.widget.insets.CustomInsetsLinearLayout>
如果需要用到RelativeLayout或FrameLayout的,把上面的继承父类改为RelativeLayout或FrameLayout就可以了。
另外,贴上一个Android键盘面板冲突 布局闪动处理方案,也就是输入法跟表情切换顺畅,跟微信一样,这个解决方法也是微信团队的人出品的,非常感谢,在这里我也要分享下给大家,因为我发现还有很多app都没解决这个问题