EditText编辑框

本文详细介绍了安卓UI中的EditText控件,讲解了如何设置其不可编辑、输入类型、提示信息、光标样式以及焦点获取等属性,并探讨了inputType的各种类型,如字符、数值等。此外,还提到了添加文本变化监听器的方法,并给出了EditText在登录界面应用的简单示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安卓UI控件之EditText编辑框的使用


前言:

EditView类继承自TextView类,EditView与TextView最大的不同就是用户可以对EditView控件进行编辑,

同时还可以为EditView控件设置监听器,用来判断用户的输入是否合法。

以下为EditView常用属性及对应方法说明


android:editable="false" 不可 编辑

android:inputType="number" 输入的类型

android:hint="请输入密码" 输入提示

setSelection 方法将输入光标移动到文本的末尾位置

android:textCursorDrawable="@null" 设置光标的颜色同字体

requestFocus() 获取焦点

backgroudTint 设置边框底线的颜色

textCursorDrawable="@null" 光标和文字颜色一致

textColorHint="#ffffff" 设置提示语的颜色



inputType 显示不同类型的键盘:

  1.字符类型:textCapCharacters 英文字符大写 textCapWords 单词首字母大写 textAutoCorrect 前两个字符自动纠正完成

        textAutoComplete 前两个字符自动完成 textUri

  2.数值类型:numberSigned 有符号数字 numberDecimal 带小数点的数字 numberPassword 数字密码

监听器:

editText.addTextChangedListener() //输入框文本发生变化时执行


下面是一个EditText的简单使用和一个登录Demo的实现:

效果如下:(这里上传的是gif图片、分辨率有点低、勉强看的清)




主Activity如下:


package com.dsl.uiapplication_04;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 功能:用Edittext实现登录输入框
 *
 * 作者:单胜凌
 * 时间:2016.12.06
 */
public class MainActivity extends AppCompatActivity {
    @Override
    private Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUi();
    }
    private void initUi()
    {
        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
           }
       });
   }

.XML文件如下:

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

    <EditText
        android:id="@+id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="账号:"
        />
    <EditText
        android:id="@+id/et2"
        android:layout_below="@id/et1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="密码:"
        />
    <Button
        android:id="@+id/button1"
        android:layout_below="@id/et2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="登录"
        />

</RelativeLayout>


下面这里是一个登录输入框的简易Demo,还是先看效果



主Activity如下:

package com.dsl.uiapplication_04;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 功能:用Edittext实现登录输入框
 *
 * 作者:单胜凌
 * 时间:2016.12.06
 */
public class MainActivity extends AppCompatActivity {
    private Button button1;
    private EditText edt1;
    private EditText edt2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edt1 = (EditText)findViewById(R.id.User_Name);
        edt2 = (EditText)findViewById(R.id.User_Password);
        button1 = (Button)findViewById(R.id.button_02);
        MyListener myListener=new MyListener();
        button1.setOnClickListener(myListener);


    }
    class MyListener implements View.OnClickListener
    {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_02: {
                    if (check()) {
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    }
                }
                break;
            }
        }
    }

    /**
     * 简单正则表达式
     * @return
     */
    public boolean check()
    {
        if(edt1.length()==0)
        {
            Toast.makeText(MainActivity.this, "请输入账号:", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(edt2.length()==0)
        {
            Toast.makeText(MainActivity.this, "请输入密码:", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}

XML文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dsl.uiapplication_04.MainActivity">

    <LinearLayout
        android:id="@+id/liner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:background="@drawable/radius_drawable_bg"
        android:orientation="vertical"
        android:padding="10dip" >

        <LinearLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@mipmap/logo_tu1" />

            <EditText
                android:id="@+id/User_Name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:background="#00000000"
                android:hint="@string/input"
                android:padding="5dip"
                android:textSize="16sp" />
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="5dip"
            android:background="@color/hui" />

        <LinearLayout
            android:id="@+id/input_layout_psw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:src="@mipmap/logo_tu2" />

            <EditText
                android:id="@+id/User_Password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:background="#00000000"
                android:hint="@string/passwordinput"
                android:inputType="textPassword"
                android:padding="5dip"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>
    <Button
        android:id="@+id/button_02"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_below="@id/liner"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_centerHorizontal="true"
        android:text="@string/login"
        android:textColor="@color/white"
        android:background="@drawable/button_drawable2"
        />
</RelativeLayout>

做出来大概就是这样子的!



关于EditText编辑框的讲解到此结束 !


源码地址如下:

https://github.com/DSLAndroid/UIApplication_04



本资源来自单胜凌!!!
Android靠自学!!!
祝各位IT人士早日取得成功!!!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值