Hi,小伙伴们,Layout
学会了,Button
和TextView
学会了,ImageView
也学会了,是否是感受老是学习这些单一的东西稍微有点枯燥了呢?那么学习了这篇文章以后,开始尽情发挥大家的想象力开始搞事情吧~php
这一篇咱们讲解EditText
的使用,EditText
是一个输入框,在Android开发中是经常使用的控件。也是获取用户数据的一种方式,EditText
是TextView
的子类,它继承了TextView
的全部属性。话很少说,让咱们赶忙开始学习吧~java
EditText的结构android
public class EditText extends TextViewgit
android.view.View ↳ android.widget.TextView ↳ android.widget.EditTextapi
1.默认提示文本的两个属性以下微信
android:hint="默认提示文本" <!--和微信须要登陆时同样,在文本框中提示的扣扣号,邮箱-->
android:textColorHint="#95A1AA" <!--提示的颜色-->
复制代码
2.得到焦点后全选组件内全部文本内容ide
android:selectAllOnFocus="true"
复制代码
3.限制EditText
输入类型布局
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" />
复制代码
4.文本类型,多为大写、小写和数字符号,可选参数以下学习
android:inputType="none" <!--无格式-->
android:inputType="phone" <!--拨号键盘-->
android:inputType="text" <!--文本格式-->
android:inputType="textAutoComplete" <!--自动完成-->
android:inputType="textAutoCorrect" <!--纠正单词的拼写错误-->
android:inputType="textCapCharacters" <!--全部字符大写-->
android:inputType="textCapSentences" <!--仅第一个字母大写-->
android:inputType="textCapWords" <!--单词首字母大写-->
android:inputType="textEmailAddress" <!--电子邮件地址格式-->
android:inputType="textEmailSubject" <!--邮件主题格式-->
android:inputType="textFilter" <!--文本筛选格式-->
android:inputType="textImeMultiLine" <!--输入法多行-->
android:inputType="textLongMessage" <!--长消息格式-->
android:inputType="textMultiLine" <!--多行输入-->
android:inputType="textNoSuggestions" <!--不提示-->
android:inputType="textPassword" <!--密码格式-->
android:inputType="textPersonName" <!--人名格式-->
android:inputType="textPhonetic" <!--拼音输入格式-->
android:inputType="textPostalAddress" <!--邮政格式-->
android:inputType="textShortMessage" <!--短消息格式-->
android:inputType="textUri" <!--URI格式-->
android:inputType="textVisiblePassword" <!--密码可见格式-->
android:inputType="textWebEditText" <!--做为网页表单的文本格式-->
android:inputType="textWebEmailAddress" <!--做为网页表单的电子邮件地址格式-->
android:inputType="textWebPassword" <!--做为网页表单的密码格式-->
复制代码
数值类型字体
android:inputType="numberPassword" <!--数字密码格式-->
android:inputType="numberSigned" <!--有符号数字格式-->
android:inputType="number" <!--数字格式-->
android:inputType="numberDecimal" <!--能够带小数点的浮点格式-->
android:inputType="phone" <!--拨号键盘-->
android:inputType="datetime" <!--日期-->
android:inputType="date" <!--日期-->
android:inputType="time" <!--时间-->
复制代码
5.设置最小行,最多行,单行
android:minLines="3" <!--设置最小行的行数-->
android:maxLines="10" <!--最大的行数,当输入内容超过maxlines,文字会自动向上滚动-->
android:singleLine="true" <!--限制EditText只容许单行输入,并且不会滚动-->
复制代码
6.设置文字间隔,设置英文字母大写类型
android:textScaleX="1.5" <!--设置字与字的水平间隔-->
android:textScaleY="1.5" <!--设置字与字的垂直间隔-->
android:capitalize="none" <!--sentences:仅第一个字母大写;words:每个单词首字母大小,用空格区分单词;characters:每个英文字母都大写-->
复制代码
7.控制EditText
四周的间隔距离与内部文字与边框间的距离
android:paddingTop="5dp" <!--使用margin相关属性增长组件相对其余控件的距离,好比android:marginTop="5dp";使用android:paddingTop="5dp"增长组件内文字和组件边框的距离-->
复制代码
8.改变输入法中回车按钮的显示内容
android:imeOptions="actionNone"
<!--imeOptions有下面一些经常使用值-->
<!--actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED;-->
<!--actionNone 没有动做,对应常量EditorInfo.IME_ACTION_NONE;-->
<!--actionGo执行 “开始” ,对应常量EditorInfo.IME_ACTION_GO;-->
<!--actionSearch 执行 “搜索”,对应常量EditorInfo.IME_ACTION_SEARCH;-->
<!--actionSend执行 “发送”,对应常量EditorInfo.IME_ACTION_SEND;-->
<!--actionNext 执行 “下一个”,对应常量EditorInfo.IME_ACTION_NEXT;-->
<!--actionPrevious 执行 “上一个”,对应常量IME_ACTION_PREVIOUS;-->
<!--actionDone 执行 “完成”,对应常量EditorInfo.IME_ACTION_DONE-->
复制代码
android:numeric="integer" <!--设置仅仅能输入整数,假设是小数则是:decimal-->
android:password="true" <!--设置仅仅能输入密码-->
android:textColor="#ff8c00" <!--字体颜色-->
android:textStyle="bold" <!--字体 bold, italic, bolditalic-->
android:textSize="20dp" <!--设置输入文本内容字体大小-->
android:textAlign="center" <!--EditText没有这个属性,但TextView有,居中-->
android:typeface="monospace" <!--字型,normal, sans, serif, monospace (标准、无衬线字体、衬线字体、等宽字体)-->
android:background="@null" <!--背景,这里设置null,意思为透明-->
android:layout_weight="1" <!--权重,控制控件之间的地位,在控制控件显示的大小时蛮实用的-->
android:cursorVisible="true" <!--设定光标为显示/隐藏,默认显示-->
android:digits="1234567890" <!--设置容许输入哪些字符,如“1234567890.+-*/% ()”-->
android:drawableRight="@drawable/xxx" <!--在EditText的右边输出一个drawable-->
android:drawableTop="@drawable/xxx" <!--在EditText的正上方输出一个drawable-->
android:drawableBottom="@drawable/xxx" <!--在EditText的下方输出一个drawable-->
android:drawableLeft="@drawable/xxx" <!--在EditText的左边输出一个drawable-->
android:drawablePadding <!--设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一块儿使用,可设置为负数,单独使用没有效果-->
android:editable="true" <!--设置是否可编辑,默承认以编辑-->
android:ellipsize="start" <!--设置当文字过长时,该控件该怎样显示,例如设置如下值:"start"省略号显示在开头,"end"省略号显示在结尾,"middle"省略号显示在中间,"marquee"以跑马灯的方式显示(动画横向移动)-->
android:gravity="center" <!--设置文本位置,如设置成"center",文本将居中显示-->
复制代码
经过上面了解EditText
输入框的经常使用属性以后,结合上几讲TextView、Button
咱们经过代码示例来说解实现一个简单的登陆页面效果
示例xml布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:layout_marginTop="10dp" android:text="登陆" android:textColor="#008577" android:textSize="30sp" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="180dp" android:layout_marginRight="20dp" android:orientation="vertical">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="账号" android:textColor="#000000" android:textSize="18sp" />
<EditText android:id="@+id/et_mobile" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_weight="1" android:background="#00000000" android:gravity="center" android:hint="请输入手机号" android:inputType="phone" android:maxLength="15" android:singleLine="true" android:textColorHint="#008577" android:textSize="18sp" />
</LinearLayout>
<View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="#FF4413" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textColor="#000000" android:textSize="18sp" />
<EditText android:id="@+id/et_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="40dp" android:layout_weight="1" android:background="#00000000" android:gravity="center" android:hint="请输入密码" android:inputType="textPassword" android:maxLength="30" android:singleLine="true" android:textColorHint="#008577" android:textSize="18sp" />
</LinearLayout>
<View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:background="#FF4413" />
</LinearLayout>
<Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="48dp" android:layout_marginTop="55dp" android:background="#81D841" android:clickable="false" android:text="登陆" android:textColor="#ffffff" android:textSize="18sp" android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
复制代码
1.设置焦点,光标的位置
EditText et = (EditText) findViewById(R.id.et_mobile);
et.setFocusable(true);
et.requestFocus();
et.setFocusableInTouchMode(true);
et.clearFocus();//失去焦点
et.requestFocus();//获取焦点
复制代码
2.设置默认输入法
et.setInputType(EditorInfo.TYPE_CLASS_TEXT); //中文键盘
et.setInputType(EditorInfo.TYPE_TEXT_VARIATION_URI); //英文键盘
et.setInputType(InputType.TYPE_CLASS_NUMBER); //数字键盘
复制代码
3.强制显示隐藏软键盘
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); //隐藏软键盘
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); //显示软键盘
复制代码
4.始终不弹出软键盘
//在XML文件中,Edittext父布局上进行以下设置
android:focusable="true"
android:focusableInTouchMode="true"
//在Java代码中,添加下面属性
et.setInputType(InputType.TYPE_NULL);
复制代码
5.显示隐藏密码
//在XML文件中设置
android:password="true" <!-- 以”.”形式显示密码文本-->
android:inputType="textPassword" <!--不可见密码-->
android:inputType="textVisiblePassword" <!--可见密码-->
//在Java代码中设置
et.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//隐藏密码
et.setTransformationMethod(PasswordTransformationMethod.getInstance());//显示密码
复制代码
6.获取文本最大长度,因为EditText
没有提供获取最大长度方法,须要用到反射
public static int getMaxLength(EditText et) {
int length = 0;
try {
InputFilter[] inputFilters = et.getFilters();
for (InputFilter filter : inputFilters) {
Class<?> c = filter.getClass();
if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
Field[] f = c.getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mMax")) {
field.setAccessible(true);
length = (Integer) field.get(filter);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return length;
}
复制代码
7.文本监听事件,监控当前输入的文本长度,须要实现TextWatcher
接口
private class TextChangedTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxLength;
private CharSequence mMobile;
public TextChangedTextWatcher(EditText v) {
super();
mView = v;
mMaxLength = getMaxLength(v);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//文本改变前
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//文本改变时
mMobile = s;
}
@Override
public void afterTextChanged(Editable s) {
//文本改变后,通常使用此方法
if (mMobile == null || mMobile.length() == 0)
return;
if (mMobile.length() == 11 && mMaxLength == 11) {
}
}
}
复制代码
看完以上内容,是否是也火烧眉毛想实现一个登陆或者注册的页面了!!那还等什么,趁着记忆力深入,让咱们一块儿开始吧,也请各位多多点赞留言哦~ 若是小伙伴想学习更多知识或者快速学习进阶,能够加入咱们的微信群一块儿探讨~ 在公众号中回复微信群,就能够加入其中,也能够在公众号中回复视频,里面有一些初学者视频哦~
PS:若是还有未看懂的小伙伴,欢迎加入咱们的QQ技术交流群:892271582,里面有各类大神回答小伙伴们遇到的问题,咱们的微信群立刻也将要和你们见面啦,届时但愿你们踊跃加入其中~~