- </LinearLayout>
运行程序效果如下:
四:输入特殊格式的字符
在我们开发程序的时候不免会输入一些特属个数的字符,比如密码(输入框的字符要加密显示),电话号码(比如数字和-),数字等,这些都算是一些特属格式的字符,强大的EditText同样为我们提供了输入这些特属格式字符的设置。
- 密码文本框。密码输入也是Android应用常用的功能,通过配置EditText的android:password="true"就可以实现这一密码输入功能,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:password=“true”/>
-
</LinearLayout>
运行效果如下:
可以看到我们输入的字符已经被“.”这样的掩码所代替。
- 手机中发短信打电话是必不可少的,所以用于专门输入电话号码的文本框也是大有用途,有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 ).通过设置android:phoneNumber="true"就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了。修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height=“wrap_content”
-
android:phoneNumber=“true”/>
-
</LinearLayout>
运行程序效果如下:
注意看软键盘,已经变成拨号专用的啦.
- 有时候我们只想输入数字,不想输入字母,EditText为我们提供了android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。这里以signed类型的为例,修改main.xml如下:
- <?xml version\="1.0" encoding\="utf-8"?>
-
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:orientation=“vertical”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”
-
>
-
<EditText
-
android:id=“@+id/edit_text”
-
android:layout_width=“fill_parent”
-
android:layout_height