【9】Android UI控件 - EditText


目录

前言

应用

默认提示文本

输入限制

获得焦点后全选组件内所有文本内容

设置行属性

设置文字间隔

设置英文字母大写类型

设置字数限制&字体效果

间隔距离与内部文字与边框间的距离

设置EditText不可编辑

登录界面

XML文件

JAVA文件

效果


前言

由下图可知,EditText 继承自 TextView,因而它和 TextView 非常类似; 最大的区别即是 EditText 可接手输入文本。

应用

默认提示文本

相关属性

android:hint                            内容为空时显示的文本

android:textColorHint            内容为空时显示的文本的颜色

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

<EditText
    android:id="@+id/ed_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="默认提示Text"
    android:textColorHint="#95A1AA"/>

</LinearLayout>

效果

输入限制

相关属性

android:digits                                             设置支持的字符

android:inputType                                      设置输入类型,该属性在 EditText 输入值时启动的虚拟键盘的风格有着重要的作用

                                                                     设置多个属性可用 | 连接

none                                                  无设置

phone                                                手机号码

text                                                    文本  

        textCapCharacters                  字母大写

        textCapWords                          首字母大写

        textCapSentences                   仅第一个字母大写

        textAutoCorrect                       自动完成

        textAutoComplete                    自动完成

        textMultiLine                            多行输入

        textImeMultiLine                      输入法多行(如果支持)

        textUri                                      网址

        textEmailAddress                    电子邮件地址

        textEmailSubject                     邮件主题

        textShortMessage                   短讯

        textLongMessage                   长信息

        textPersonName                     人名

        textPostalAddress                  地址

        textPassword                          密码

        textVisiblePassword              可见密码

        textWebEditText                     作为网页表单的文本

        textFilter                                 文本筛选过滤

        textPhonetic                           拼音输入

number                                            数字

        numberSigned                        带符号数字格式

        numberDecimal                      带小数点的浮点格式

        phone                                      拨号键盘

        datetime                                  时间日期

        date                                         日期键盘

        time                                         时间键盘

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

    <!--数字账号0-9-->
    <EditText
        android:id="@+id/ed_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号"
        android:textColorHint="#95A1AA"
        android:inputType="numberDecimal"
        android:digits="0123456789"
        />

    <!--密码-->
    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:textColorHint="#95A1AA"
        android:inputType="textPassword"
        />

</LinearLayout>

效果

获得焦点后全选组件内所有文本内容

相关属性

android:selectAllOnFocus                    可设置为 true / false

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

    <!--数字账号-->
    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号-普普通通"
        android:textColorHint="#95A1AA"
        android:inputType="numberDecimal"
        android:digits="0123456789"/>

    <!--数字账号 获得焦点全选-->
    <EditText
        android:id="@+id/ed_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号-获得焦点全选"
        android:textColorHint="#95A1AA"
        android:inputType="numberDecimal"
        android:digits="0123456789"
        android:selectAllOnFocus="true"/>

</LinearLayout>

效果图

设置行属性

EditText默认是多行显示的,并且能够自动换行。

此处仅作最大最小行补充;单行、多行、自动换行等属性,请看我上一章 TextView-自动换行部分

当输入内容超过maxline,文字会自动向上滚动。

若想取消向上滚动效果并设置单行,则需要关闭 自动换行。

相关属性

android:maxLines        最大行

android:minLines         最小行

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:gravity="center">

    <!--账号-->
    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号-平平无奇"
        android:textColorHint="#95A1AA"
        android:inputType="text" />

    <!--账号-->
    <EditText
        android:id="@+id/ed_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号-不允许自动换行"
        android:textColorHint="#95A1AA"
        android:singleLine="true" />

    <!--账号-->
    <EditText
        android:id="@+id/ed_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号-最大行5最小行4"
        android:maxLines="5"
        android:minLines="4"
        android:textColorHint="#95A1AA" />

</LinearLayout>

效果图

设置文字间隔

在上一章 TextView-字句行距 中讲到一个属性 textScaleX:控制字体水平方向的缩放。

与其类似的属性有 textScaleY:控制字体竖直方向的缩放。

android:textScaleX        控制水平间隔

android:textScaleY        控制垂直间隔

设置英文字母大写类型

capitalize 对应的全局属性资源符号是 bufferType

全局属性 capitalizeAPI 3 被弃用,使用 inputType 代替。

故而只介绍,不做示例。

android:capitalize          设置英文字母大写类型的属性

        sentences               仅第一个字母大写

        words                      每一个单词首字母大小,用空格区分单词

        characters               每一个英文字母都大写

 

设置字数限制&字体效果

相关属性

android:maxLength

android:typeface

     normal            正常

     sans               非衬线字体

     serif                衬线字体

     monospace    等宽字体

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入框"
        android:textColorHint="#95A1AA"
        android:maxLength="2"
        android:typeface="normal" />

    <EditText
    android:id="@+id/ed_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="输入框"
    android:textColorHint="#95A1AA"
    android:maxLength="2"
    android:typeface="sans" />

    <EditText
        android:id="@+id/ed_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入框"
        android:textColorHint="#95A1AA"
        android:maxLength="2"
        android:typeface="serif" />

    <EditText
        android:id="@+id/ed_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="输入框"
        android:textColorHint="#95A1AA"
        android:maxLength="2"
        android:typeface="monospace" />

</LinearLayout>

效果

间隔距离与内部文字与边框间的距离

相关属性

margin 相关属性增加组件相对其他控件的距离 ---- android:marginTop

padding 增加组件内文字和组件边框的距离 ---- android:paddingTop

注意

这两个都可以设置边距,但有细微的区别:

(1) android:padding 是相对父 View 的边距

(2) android:layout_margin 是相对同一级 View 的边距

示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="原输入框"
        android:textColorHint="#95A1AA"
        android:layout_margin="50dp" />

    <EditText
        android:id="@+id/ed1.1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="控件距离"
        android:textColorHint="#95A1AA"
        android:layout_margin="50dp" />

    <EditText
        android:id="@+id/ed_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="组件内文字和组件边框"
        android:padding="80dp"
        android:textColorHint="#95A1AA" />

</LinearLayout>

效果图

设置EditText不可编辑

请戳上文超链接。

登录界面

本例程使用的相关属性均在前面的章节中有所描述,至于点击事件和页面跳转则将后面讲解。

XML文件

登录界面 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:padding="30dp">

    <!--数字账号0-9-->
    <EditText
        android:id="@+id/ed_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="账号"
        android:background="@drawable/test_1"
        android:textColorHint="#95A1AA"
        android:inputType="numberDecimal"
        android:digits="0123456789"
        android:layout_marginTop="40dp"
        android:drawableLeft="@drawable/user"
        android:padding="10dp"/>

    <!--密码-->
    <EditText
        android:id="@+id/ed_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="密码"
        android:background="@drawable/test_1"
        android:textColorHint="#95A1AA"
        android:inputType="textPassword"
        android:drawableLeft="@drawable/suo"
        android:padding="10dp"
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/b1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="登录"
        />

</LinearLayout>

欢迎界面activity_welcome.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:id="@+id/v1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="欢迎光临"
        android:textSize="50dp"
        android:background="#ffff88"/>

</LinearLayout>

JAVA文件

MainActivity.java

package com.example.test_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    private Button butt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //通过id获取Button对象
        butt = findViewById(R.id.b1);
        //点击事件
        butt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转界面
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
    }

}

Main2Activity.java

package com.example.test_ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

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

效果

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值