• EditText 基本用法



    title: EditText 基本用法
    tags: EditText,编辑框,输入框

    EditText介绍:

    EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是用户跟Android应用进行数据传输的窗户,比如实现一个登陆界面,需要用户输入账号密码,然后我们获取用户输入的内容,提交给服务器进行判断。

    EditText 支持的 XML 属性及相关方法

    XML 属性 相关方法 说明
    android:text setText(CharSequence text) 设置文本内容
    android:textColor setTextColor(int color) 字体颜色
    android:hint setHint(int resid) 内容为空时候显示的文本
    android:textColorHint void setHintTextColor(int color) 为空时显示的文本的颜色
    android:inputType setInputType(int type) 限制输入类型
    number:整数类型
    numberDecimal:小数点类型
    date:日期类型
    text:文本类型(默认值)
    phone:拨号键盘
    textPassword:密码
    textVisiblePassword:可见密码
    textUri:网址
    android:maxLength 限制显示的文本长度,超出部分不显示
    android:minLines setMaxLines(int maxlines) 设置文本的最小行数
    android:gravity setGravity(int gravity) 设置文本位置,如设置成“center”,文本将居中显示。
    android:drawableLeft setCompoundDrawables(Drawable left,Drawable top,Drawable right, Drawable bottom) 在text的左边输出一个drawable,如图片
    android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
    android:digits 设置允许输入哪些字符。如“1234567890”
    android:ellipsize 设置当文字过长时,该控件该如何显示。
    start:省略号显示在开头
    end:省略号显示在结尾
    middle:省略号显示在中间
    marquee:以跑马灯的方式显示(动画横向移动)
    android:lines setLines(int lines) 设置文本的行数,设置两行就显示两行,即使第二行没有数据。
    android:lineSpacingExtra 设置行间距
    android:singleLine setSingleLine() true:单行显示 false:可以多行
    android:textStyle 设置字形,可以设置一个或多个,用"|"隔开
    bold:粗体
    italic:斜体
    bolditalic:又粗又斜

    EditText实例:开发中常用的登录界面

    首先我们来看布局文件:activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:background="@null"
            android:inputType="number"
            android:maxLength="11"
            android:hint="请输入手机号"
            android:drawablePadding="10dp"
            android:padding="10dp"
            android:drawableLeft="@mipmap/icon_phone"
            android:drawableBottom="@drawable/shape_et_bottom_line"
            android:layout_marginTop="20dp"/>
    
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:background="@null"
            android:inputType="textPassword"
            android:maxLength="16"
            android:padding="10dp"
            android:drawablePadding="10dp"
            android:hint="请输入密码"
            android:drawableBottom="@drawable/shape_et_bottom_line"
            android:drawableLeft="@mipmap/icon_password"/>
    
        <TextView
            android:id="@+id/tv_login"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="30dp"
            android:text="登 录"
            android:textColor="#ffffffff"
            android:textSize="18sp" />
    </LinearLayout>
    

    运行效果图如下:
    带有EditeText的登录界面

    这两个输入框的用的的大部分属性都在上面的表格中了,我这里解决下没有说过的属性。
    android:background="@null" 输入框无背景
    android:drawableBottom="@drawable/shape_et_bottom_line" 底部引入一个shape布局文件,这个布局文件就是输入框的下划线。
    shape_et_bottom_line.xml内容如下:

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="#1E7EE3" />
        <size android:height="1dp" android:width="500dp"/>
    
    </shape>
    

    EditeText还有哪些功能?

    1.监听用户输入的内容.
    有这样一个场景,一个搜索框,只要用户输入了内容就去请求服务器,于是我们在Activity里面监听EditeText文本改变事件。

    EditText etOne= (EditText) findViewById(R.id.et_phone);
    etOne.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    Log.i("Ansen","内容改变之前调用:"+s);
                }
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    Log.i("Ansen","内容改变,可以去告诉服务器:"+s);
                }
    
                @Override
                public void afterTextChanged(Editable s) {
                    Log.i("Ansen","内容改变之后调用:"+s);
                }
     });
    

    首先我们通过id找到EditText控件,并且添加监听函数,内部内实现TextWatcher接口,重写三个方法。我们可以在onTextChanged方法中告诉服务器我要搜索的内容。

    源码下载

    点击下载源码

    各位看官如果觉得文章不错,帮忙点个赞吧,对于你来说是举手之劳,但对于我来说这就是坚持下去的动力。

    如果你想第一时间看我们的后期文章,扫码关注公众号,每周不定期推送Android开发实战教程文章,你还等什么,赶快关注吧,学好技术,出任ceo,赢取白富美。。。。

          Android开发666 - 安卓开发技术分享
                扫描二维码加关注
    

    Android开发666

  • 相关阅读:
    BufferedImage学习记录一
    response总结一
    Externalizable接口
    request 总结一
    处理jsp显示文字过长问题
    验证码设计
    ORA01461: 仅能绑定要插入 LONG 列的 LONG 值
    MAP平台在单据中填写好部门后,关闭后重新打开,部门就没有了
    MAP平台设置节点选取范围
    MAP平台java.lang.StackOverflowError
  • 原文地址:https://www.cnblogs.com/yishaochu/p/5785234.html
Copyright © 2020-2023  润新知