Android基础控件EditText的使用

1.简单介绍

EditText是文本编辑框,用户可以在此输入文本等信息,下图是例子的显示效果。
显示效果
EditText的常用属性如下。

  • inputType:指定文本输入的类型,代码中的方法是setInputType。如果要同时使用多种类型,则通过“|”连接起来。取值如下表。
  • maxLength:指定输入文本的最大长度。该属性无法通过代码设置。
  • hint:指定提示文本的内容,代码中对应的方法是setHint。
  • textColorHint:指定提示文字的颜色,代码中对应的方法是setHintTextColor。
    inputType取值

2.简单使用

下面展示最原始的EditText、通过设置属性cursorVisible使光标不可见的EditText、通过设置属性textCursorDrawable更换了光标图标的EditText、通过设置属性background为@null而没有边框的EditText和通过设置background属性自定义边框的EditText。下面是实现的代码。

  1. shape_edit_focus.xml。更换边框时EditText获取焦点时的边框。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--圆角-->
    <corners android:radius="5dp"/>
    <!--描边-->
    <stroke
        android:width="1dp"
        android:color="#0000ff"/>
    <!--与周围视图的距离-->
    <padding android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp"/>
</shape>
  1. shape_edit_normal.xml。更换边框时EditText普通状态下的边框。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--内部填充的颜色-->
    <solid android:color="#ffffff"
        />

    <!--描边-->
    <stroke android:width="1dp"
        android:color="#aaaaaa"/>

    <!--圆角-->
    <corners android:radius="5dp"/>

    <!--与周围视图的距离-->
    <padding android:bottom="2dp"
        android:top="2dp"
        android:right="2dp"
        android:left="2dp"/>
</shape>
  1. edit_text_selector.xml。定义边框的xml文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--获得焦点(用户点击了这个EditText)-->
    <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>

    <!--普通状态-->
    <item android:drawable="@drawable/shape_edit_normal"/>
</selector>
  1. activity_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditTextActivity"
    android:orientation="vertical">

    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是最原始的EditText"
        android:maxLength="15"
        />

    <!--cursorVisible属性设置光标是否可见-->
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是光标不可见的EditText"
        android:cursorVisible="false"/>

    <!--textCursorDrawable属性更换光标图标-->
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是更换了光标的EditText"
        android:textCursorDrawable="@drawable/text_cursor"/>

    <!--background属性设为为@null使其边框消失-->
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是没有边框的EditText"
        android:background="@null"
        android:textCursorDrawable="@drawable/text_cursor"/>

    <!--通过background属性更换边框-->
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="这是更换了框框的EditText"
        android:background="@drawable/edit_text_selector"
        android:textCursorDrawable="@drawable/text_cursor"
        android:inputType="number"
        android:maxLength="15"/>
</LinearLayout>
  1. EditTextActivity.java
package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class EditTextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_text);
    }
}

3.更多

  1. 若不想一打开应用EditText就获取焦点,可以设置根节点的focusable和focusableInTouchMode属性为true让根节点获取焦点。
  2. 输入完成后自动隐藏输入法(如输入11位手机号之后自动隐藏输入法)。戳这里
  3. 输入回车焦点自动转移到下一个控件。戳这里
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值