EditText概述:编辑文本控件,具备TextView控件所有属性,还实现输入文本内容。

使用EditText做字符限制和校验,字符限制与校验在日常开发中,使用很频繁,登陆注册,填写单据内容等...

我写了一个demo来实现字符限制和校验,效果图如下:


效果图一:主视图

EditText字符限制与校验


效果图二:digits和inputType做字符限制对比

EditText字符限制与校验EditText字符限制与校验


效果图三:当内容为空,提示Error信息

EditText字符限制与校验


代码展示

MainActivit代码:

package com.example.verify;

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

public class MainActivity extends AppCompatActivity {
    private EditText et_ver;
    private Button bt_ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_ver=(EditText) findViewById(R.id.et_ver);
        bt_ok=(Button) findViewById(R.id.bt_ok);
        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CharSequence text=et_ver.getText();
                if (TextUtils.isEmpty(text)) {
                    et_ver.setError("不能为空!!");
                }
            }
        });
    }
}

Layout代码:

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/et_one"/>

    <EditText
        android:id="@+id/et_ver"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:digits="abc"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/et_two"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"/>

    <Button
        android:id="@+id/bt_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/bt_ok"/>

</LinearLayout>


尾声:有兴趣探讨或疑问,可以在下面留言评论



相关文章:

  • 2021-11-12
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2022-12-23
  • 2021-09-01
猜你喜欢
  • 2022-01-20
  • 2021-09-17
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案