TextView 直接继承View类, 同时是 EditText 和 Button 两组组件类的父类; 

      EditText是一个非常重要的组件,可以说它是用户和Android应用进行数据传输窗户,有了它就等于有了一扇和Android应用传输的门,通过它用户可以把数据传给Android应用,然后得到我们想要的数据。

EditText是TextView的子类,所以TextView的方法和特性同样存在于EditText中。

界面:

3.4文本域-冯金伟博客园3.4文本域-冯金伟博客园

资源:

string.xml

<resources>
    <string name="app_name">TextFieldLiang</string>
    <string-array name="namearr">
        <item>manusas2</item>
        <item>MANusbs2</item>
        <item>manuscs2</item>
        <item>liang2</item>
    </string-array>
</resources>

布局:

<?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.liang.textfieldliang.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="普通输入框"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="密码输入框"
        android:ems="10"
        android:id="@+id/editText2"
        android:layout_below="@+id/editText"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="邮件输入框(测试专用)"
        android:ems="10"
        android:id="@+id/editText3"
        android:layout_below="@+id/editText2"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editText2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:hint="电话输入框"
        android:ems="10"
        android:id="@+id/editText4"
        android:layout_below="@+id/editText3"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editText3" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="数字输入框"
        android:ems="10"
        android:id="@+id/editText5"
        android:layout_below="@+id/editText4"
        android:layout_alignParentStart="true"
        android:layout_alignEnd="@+id/editText4" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="验证"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="62dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="your name:"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

    <AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autoCompleteTextView"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/textView" />

</RelativeLayout>

代码:

package com.liang.textfieldliang;

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

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button button;
    private AutoCompleteTextView autoCompleteTextView;
    private static final String[] NAMESTATIC = new String[]{
            "manusas3", "manusbs3", "manuscs3", "liang3"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //邮箱
        editText = (EditText) this.findViewById(R.id.editText3);
        button = (Button) this.findViewById(R.id.button);
        autoCompleteTextView = (AutoCompleteTextView) this.findViewById(R.id.autoCompleteTextView);
        //点击监听
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString().trim();
                validateEmail(content);
            }
        });

        //键盘key监听
        editText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //按键 监听 asc码
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    Toast.makeText(MainActivity.this, "key enter:" + keyCode, Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

        //失去焦点
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                String content = editText.getText().toString().trim();
                if (hasFocus == false) {
                    validateEmail(content);
                }
            }
        });

        //都是开头匹配 不区分大小写
/*        //方法一 new一个实例
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getStrList());
        // ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,NAMESTATIC);
        //将适配器的内容放到autocompletetextview中
        autoCompleteTextView.setAdapter(adapter);*/

        //方法二 string.xml读取数组
        //String[] strarr=getResources().getStringArray(R.array.namearr);
        //ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strarr);
        // autoCompleteTextView.setAdapter(adapter);

        //方法三 string.xml读取数组
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.namearr, android.R.layout.simple_list_item_1);
        autoCompleteTextView.setAdapter(adapter);
    }

    private List<String> getStrList() {
        List<String> list = new ArrayList<>();
        list.add("manusas1");
        list.add("manusbs1");
        list.add("manuscs1");
        list.add("liang1");
        return list;
    }

    private void validateEmail(String content) {
        if (content != null && !"".equals(content)) {
            //电子邮件 正则
            String mailcheck = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Pattern regex = Pattern.compile(mailcheck);
            Matcher matcher = regex.matcher(content);
            boolean isMatched = matcher.matches();
            if (isMatched) {
                Toast.makeText(MainActivity.this, "邮件格式正确", Toast.LENGTH_SHORT).show();
            } else {
                //setError错误提示
                editText.setError("邮件格式不对");
            }
        } else {
            editText.setError("请输入邮件");
        }
    }


}