EditText 基本用法

EditText 在开发中会经常用到,它是用户跟Android进行数据传输的入口,如果我们要实现一个登陆界面,就需要输入用户名密码,然后我们获取输入的内容,提交给服务器进行后续判断。

1.android:text 设置文本内容

2.android:textColor 设置字体颜色 

3.android:hint 设置内容为空时候显示的文本

4.android:textColorHint 设置内容为空时显示的文本的颜色

5.android:inputType 设置限制输入类型
number:整数类型
numberDecimal:小数点类型
date:日期类型
text:文本类型(默认值)
phone:拨号键盘
textPassword:密码
textVisiblePassword:可见密码
textUri:网址

6.android:maxLength 限制显示的文本长度,超出部分不显示

7.android:minLines 设置文本的最小行数

8.android:gravity 设置文本位置,如设置成“center”,文本将居中显示

9.android:layout_gravity 设置内部文本的居中方式

10.android:drawableLeft 在text的左边输出一个drawable,如图片

11.android:drawablePadding 设置text与drawable(图片)的间隔,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。

12.android:digits 设置允许输入哪些字符。如“1234567890”

13.android:ellipsize 设置当文字过长时,该控件该如何显示
start:省略号显示在开头
end:省略号显示在结尾
middle:省略号显示在中间
marquee:以跑马灯的方式显示(动画横向移动)

14.android:lines 设置文本的行数,设置两行就显示两行,即使第二行没有数据

15.android:lineSpacingExtra 设置行间距

16.android:singleLine android:singleLine

17.android:textStyle 设置字形,可以设置一个或多个,用"|"隔开
bold:粗体
italic:斜体
bolditalic:又粗又斜

输入框的用的的大部分属性都在上面的表格中

<?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>

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方法中告诉服务器我要搜索的内容

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

带刀走江湖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值