Android EditText常用属性

一、EditText介绍

  ①EditText是一个输入框,在Android开发中是常用的控件。也是获取用户数据的一种方式。

  ②EditText是TextView的子类,它继承了TextView的所有属性。

  

二、常用属性 

1 输入类型:android:inputType="value" value列表


 

①number   只能输入数字

②numberDecimal  只能输入浮点数(小数)整数

③带password  将输入的文字显示···,用户输入密码

④textMultiLine 多行输入

⑤textNoSuggestions  无提示

 

2 设置不可编辑  android:editable="false"
   true 表示可以编辑
   false 表示不可编辑

 

3 提示文字 android:hint="密码"

 

三、常用方法

1 设置焦点,光标的位置

    et.setFocusable(true);
    et.requestFocus();
    et.setFocusableInTouchMode(true);

 

2 文本监听事件

  et.addTextChangedListener(new TextWatcher() {
   @Override
   public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
   //文本改变前
  }
  
   @Override
   public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
   //文本改变时
   }

   @Override
   public void afterTextChanged(Editable editable) {
   //文本改变后,一般使用此方法
   }
  });

3 设置EditText不可编辑但可拖动查看内容
 

Android 不可编辑单行显示能滑动查看内容

 

 四、练习

   【效果】结合其他属性和控件,编写登录界面

      

   【代码】 

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     xmlns:app="http://schemas.android.com/apk/res-auto"
  4     xmlns:tools="http://schemas.android.com/tools"
  5     android:layout_width="match_parent"
  6     android:layout_height="match_parent"
  7     tools:context=".view.LoginActivity"
  8     android:background="@drawable/login_main_bg2">
  9 
 10     <LinearLayout
 11         android:layout_width="match_parent"
 12         android:layout_height="match_parent"
 13         android:background="#3fa0a0a0"
 14         android:gravity="center"
 15         android:orientation="vertical">
 16 
 17         <ImageView
 18             android:id="@+id/change_user"
 19             android:layout_width="100dp"
 20             android:layout_height="100dp"
 21             android:layout_gravity="center_horizontal"
 22             android:layout_marginBottom="24dp"
 23             android:src="@drawable/next" />
 24 
 25 
 26         <RelativeLayout
 27             android:layout_width="match_parent"
 28             android:layout_height="wrap_content">
 29 
 30             <EditText
 31                 android:id="@+id/user_name"
 32                 android:layout_width="match_parent"
 33                 android:layout_height="50sp"
 34                 android:layout_margin="10dp"
 35                 android:background="@drawable/login_input_bg"
 36                 android:gravity="center"
 37                 android:hint="用户名"
 38                 android:inputType="number"
 39                 android:padding="5dp" />
 40 
 41             <Button
 42                 android:id="@+id/rl_user"
 43                 android:layout_width="wrap_content"
 44                 android:layout_height="wrap_content"
 45                 android:layout_alignParentRight="true"
 46                 android:layout_centerVertical="true"
 47                 android:background="@null"
 48 
 49                 />
 50         </RelativeLayout>
 51 
 52 
 53         <EditText
 54             android:id="@+id/user_password"
 55             android:layout_width="match_parent"
 56             android:layout_height="50sp"
 57             android:layout_margin="10dp"
 58             android:background="@drawable/login_input_bg"
 59             android:gravity="center"
 60             android:hint="密码"
 61             android:inputType="textPassword"
 62             android:padding="5dp" />
 63 
 64         <LinearLayout
 65             android:layout_width="match_parent"
 66             android:layout_height="wrap_content"
 67             android:layout_marginLeft="10dp"
 68             android:layout_marginTop="10dp">
 69 
 70             <CheckBox
 71                 android:id="@+id/cb"
 72                 android:layout_width="wrap_content"
 73                 android:layout_height="wrap_content"
 74                 android:background="@drawable/login_input_bg"
 75                 android:padding="10dp"
 76                 android:text="记住密码"
 77                 android:textSize="18sp" />
 78 
 79         </LinearLayout>
 80 
 81 
 82         <Button
 83             android:id="@+id/btn_denglu"
 84             android:layout_width="180dp"
 85             android:layout_height="80dp"
 86             android:layout_gravity="right"
 87             android:layout_marginTop="30dp"
 88             android:background="@drawable/next" />
 89     </LinearLayout>
 90 
 91     <Button
 92         android:id="@+id/btn_zhuche"
 93         android:layout_width="match_parent"
 94         android:layout_height="wrap_content"
 95         android:layout_alignParentBottom="true"
 96         android:gravity="center"
 97         android:textColor="#050505"
 98         android:text="还没有账号? 去创建"
 99         android:textSize="18sp"
100         android:background="@null"/>
101 
102 </RelativeLayout>

 

 

  

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EditText继承关系:View-->TextView-->EditText   EditText属性很多,这里介绍几个: android:hint="请输入数字!"//设置显示在空间上的提示信息 android:numeric="integer"//设置只能输入整数,如果是小数则是:decimal android:singleLine="true"//设置单行输入,一旦设置为true,则文字不会自动换行。 android:password="true"//设置只能输入密码 android:textColor = "#ff8c00"//字体颜色 android:textStyle="bold"//字体,bold, italic, bolditalic android:textSize="20dip"//大小 android:capitalize = "characters"//以大写字母写 android:textAlign="center"//EditText没有这个属性,但TextView有,居中 android:textColorHighlight="#cccccc"//被选中文字的底色,默认为蓝色 android:textColorHint="#ffff00"//设置提示信息文字的颜色,默认为灰色android:textScaleX="1.5"//控制字与字之间的间距 android:typeface="monospace"//字型,normal, sans, serif, monospace android:background="@null"//背景,这里没有,指透明 android:layout_weight="1"//权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。 android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外观 android:layout_gravity="center_vertical"//设置控件显示的位置:默认top,这里居中显示,还有bottom android:gravity="top" //多行中指针在第一行第一位置 et.setSelection(et.length());//调整光标到最后一行 android:autoText //自动拼写帮助 android:capitalize //首字母大写 android:digits //设置只接受某些数字 Android:singleLine//是否单行或者多行,回车是离开文本框还是文本框增加新行 android:numeric //只接受数字 android:phoneNumber //输入电话号码 android:editable //是否可编辑 android:autoLink=”all” //设置文本超链接样式当点击网址时,跳向该网址 android:textAppearance="?android:attr/textAppearanceLargeInverse"//文字外观,这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。不知道这样理解对不对?  属性名称描述   android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)   android:autoText如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。   android:bufferType指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符,   也就是说getText后可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,参见这里1、这里2。   android:capitalize设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。   android:cursorVisible设定光标为显示/隐藏,默认显示。   android:digits设置允许输入哪些字符。如“1234567890.+-*/% ()”   android:drawableBottom在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。   android:drawableLeft在text的左边输出一个drawable,如图片。   android:drawablePadding设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。 android:drawableRight在text的右边输出一个drawable,如图片。 android:drawableTop在text的正上方输出一个drawable,如图片。 android:editable设置是否可编辑。这里无效果,参见EditView。 android:editorExtras设置文本的额外的输入数据。在EditView再讨论。 android:ellipsize设置当文字过长时,该控件该如何显示。有如下值设置:”start”—?省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动) android:freezesText设置保存文本的内容以及光标的位置。参见:这里。 android:gravity设置文本位置,如设置成“center”,文本将居中显示。 android:hintText为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在EditView中使用,但是这里也可以用。 android:imeOptions附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中再详细说明,此处无用。 android:imeActionId设置IME动作ID。在EditView再做说明,可以先看这篇帖子:这里。   android:imeActionLabel设置IME动作标签。在EditView再做说明。   android:includeFontPadding设置文本是否包含顶部和底部额外空白,默认为true。   android:inputMethod为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin,但是这里报错找不到。   android:inputType设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中再详细说明,这里无效果。   android:linksClickable设置链接是否点击连接,即使设置了autoLink。   android:marqueeRepeatLimit在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。   android:ems设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度,如图: android:maxEms设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。 android:minEms设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。   android:maxLength限制显示的文本长度,超出部分不显示。   android:lines设置文本的行数,设置两行就显示两行,即使第二行没有数据。   android:maxLines设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。   android:minLines设置文本的最小行数,与lines类似。   android:lineSpacingExtra设置行间距。   android:lineSpacingMultiplier设置行间距的倍数。如”1.2”   android:numeric如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。   android:password以小点”.”显示文本   android:phoneNumber设置为电话号码的输入方式。   android:privateImeOptions设置输入法选项,此处无用,在EditText将进一步讨论。   android:scrollHorizontally设置文本超出TextView的宽度的情况下,是否出现横拉条。   android:selectAllOnFocus如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。TextView中设置后无效果。   android:shadowColor指定文本阴影的颜色,需要与shadowRadius一起使用。效果: android:shadowDx设置阴影横向坐标开始位置。   android:shadowDy设置阴影纵向坐标开始位置。   android:shadowRadius设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。   android:singleLine设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行 android:shadowDx设置阴影横向坐标开始位置。   android:shadowDy设置阴影纵向坐标开始位置。   android:shadowRadius设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。   android:singleLine设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text="test_ singleLine " android:singleLine="true" android:layout_width="20dp"将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行   android:text设置显示文本.  android:textSize设置文字大小,推荐度量单位”sp”,如”15sp”   android:textStyle设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开   android:typeface设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]   android:height设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)   android:maxHeight设置文本区域的最大高度   android:minHeight设置文本区域的最小高度   android:width设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width的区别看这里。   android:maxWidth设置文本区域的最大宽度   android:minWidth设置文本区域的最小宽度   android:textAppearance设置文字外观。如“?android:attr/textAppearanceLargeInverse   ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse   android:textAppearance设置文字外观。如“?android:attr/textAppearanceLargeInverse ”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInver

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值