edittext 手機號、郵箱輸入限制


 
 
package com.example.yanlei.myapplication;

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

public class MainActivity extends AppCompatActivity {

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

}
/*顯示點擊的內容*/
private void showClickMessage(String message) {
Toast.makeText(MainActivity.this, "你選擇的是: " + message, Toast.LENGTH_LONG).show();
}

public void click(View view) {
EditText et = (EditText) findViewById(R.id.editText);
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//Button btn_Button = (Button)(view);
//showClickMessage("ok" + btn_Button.getText());

}
}
<?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:id="@+id/activity_main"
    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.example.yanlei.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我愛你 YL!"
        android:textStyle="normal|bold|italic"
        android:textColor="@color/colorAccent" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="49dp"
        android:layout_marginStart="49dp"
        android:id="@+id/textView2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Name"
        android:ems="10"
        android:layout_below="@+id/textView"
        android:layout_marginTop="66dp"
        android:id="@+id/editText" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginBottom="31dp"
        android:onClick="click"
        android:id="@+id/button2" />
</RelativeLayout>

  

 
 
 
 
本文章來給大家介紹在android開發中我們要對EditText限制,只能讓用戶輸入像數字 字母 郵箱地址,電話號之類的,其它的不能輸入。
 

下面以數字、電話為例講述EditText怎么設置輸入類型,其他類型可以參考InputType類。

1) 只能輸入數字

 代碼如下 復制代碼

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_CLASS_NUMBER);

2) 只能輸入電話號碼

 代碼如下 復制代碼

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_CLASS_PHONE);//電話

3) 郵箱地址

 代碼如下 復制代碼

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

4) 禁止輸入任何文本

 代碼如下 復制代碼

EditText et = (EditText) findViewById(R.id.etTest);
et.setInputType(InputType.TYPE_NULL);

// 禁止輸入(不彈出輸入法)上述也是隱藏輸入法的一種方式,還有另外一種隱藏辦法,

可查看android隱藏IME(輸入法)輸入框

不讓程序默認升起IME輸入框有兩種方法: 
1.讓EditText失去焦點,使用EditText的clearFocus方法 
2.強制隱藏Android輸入法窗口,在IME類中我們通過實例化輸入法控制對象,通過hideSoftInputFromWindow來隱藏IME輸入框。

代碼

 

 

 代碼如下 復制代碼
Toast.makeText(WindowBackgroundColorActivity.this, "焦點改變", Toast.LENGTH_SHORT).show(); 
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
//第一種方法 
//imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); 
//第二種方法 
imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM