• EditText 使用详解


    本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

    一、EditText 继承关系

    二、EditText 常用举例

    三、EditText 自定义背景框

    四、EditText自动检测输入内容

    五、Edittext 密文显示

    六、EditText 限制只能输入特定字符

    七、EditText 输入保存的字符串不能为空

    一、EditText 继承关系

    EditText继承关系 如下:

    java.lang.Object    
           ↳ android.view.View     
                  ↳ android.widget.TextView      
                          ↳ android.widget.EditText
    

    二、EditText 常用举例

    EditText主要用于输入和修改文本内容。

    限制只能输入纯文本内容举例如下:

    <EditText
          android:id="@+id/plain_text_input"
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:inputType="text"/>
    

    三、EditText 自定义背景框

    • xml 中使用EditText 控件
        <!-- 自定义EditText 背景 -->
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/custom_edittext_background"
            android:gravity="center"
            android:hint="一、自定义EditText背景框"
            android:padding="8dp"
            android:textSize="16sp" />
    
    • 自定义 EditText 背景框
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <!-- 圆角-->
        <corners android:radius="5dp" />
        <!--描边-->
        <stroke
            android:width="1dp"
            android:color="@android:color/holo_blue_light" />
    
    </shape>
    
    • 实现效果
      image

    四、EditText自动检测输入内容

    • xml 中使用EditText 控件
    <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:autoText="true"
            android:hint="二、自动检测输入更正属性 autoText"
            android:textColor="#FF6100" />
    
    • 实现效果

    image

    五、Edittext 密文显示

    • xml 中使用EditText 控件
    <!-- 以密文的形式显示 -->
    
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="三、以密文的形式显示密码"
            android:password="true" />
    
    • 实现效果

    image

    六、EditText 限制只能输入特定字符

    限定只能输入阿拉伯数字实现如下:

    • xml 中使用EditText 控件
      <!-- 设置允许输入的字符 -->
    
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:digits="123456789.+-*/
    ()"
            android:hint="四、设置限制允许输入阿拉伯数字" />
    
    • 实现效果

    image

    七、EditText 输入保存的字符串不能为空

    EditText常用来获取用户输入内容,因为我们要规避用户输入的内容为空的情况。

    实现效果如下:

    image

    实现代码如下:

  • 相关阅读:
    基于nginx的rtmp的服务器(nginx-rtmp-module)
    基于nginx的HLS简单服务器搭建
    HLS(HTTP Live Streaming)协议之m3u8文件生成方式
    基于live555的一个简单RTSP服务器
    基于webrtc的多人视频会话的demo运行程序
    写给小白的Python之019:面向对象-类方法、静态方法
    写给小白的Python之018:面向对象-私有成员、@property
    写给小白的Python之017:面向对象-封装、继承、多态
    写给小白的Python之016:面向对象-魔法方法
    写给小白的Python之015:面向对象-类和对象
  • 原文地址:https://www.cnblogs.com/linhaostudy/p/14068023.html
Copyright © 2020-2023  润新知