前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >EditText的属性和使用方法

EditText的属性和使用方法

作者头像
分享达人秀
发布2018-02-02 17:38:22
2.2K0
发布2018-02-02 17:38:22
举报
文章被收录于专栏:分享达人秀分享达人秀

EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法。EditText与TextView的最大区别在于:EditText可以接受用户输入。

一、EditText简介

EditText支持的XML属性及相关方法见TextView表中介绍的与输入有关的属性和方法,其中比较重要的一个属性是inputType,用于为EditText设置输入类型,其属性值主要有以下一些。

n android:inputType="none":普通字符。

n android:inputType="text":普通字符。

n android:inputType="textCapCharacters":字母大写。

n android:inputType="textCapWords":首字母大写。

n android:inputType="textCapSentences":仅第一个字母大写。

n android:inputType="textAutoCorrect":自动完成。

n android:inputType="textAutoComplete":自动完成。

n android:inputType="textMultiLine":多行输入。

n android:inputType="textImeMultiLine":输入法多行(如果支持)。

n android:inputType="textNoSuggestions":不提示。

n android:inputType="textUri":网址。

n android:inputType="textEmailAddress":电子邮件地址。

n android:inputType="textEmailSubject":邮件主题。

n android:inputType="textShortMessage":短讯。

n android:inputType="textLongMessage":长信息。

n android:inputType="textPersonName":人名。

n android:inputType="textPostalAddress":地址。

n android:inputType="textPassword":密码。

n android:inputType="textVisiblePassword":可见密码。

n android:inputType="textWebEditText":作为网页表单的文本。

n android:inputType="textFilter":文本筛选过滤。

n android:inputType="textPhonetic":拼音输入。

n android:inputType="number":数字。

n android:inputType="numberSigned":带符号数字格式。

n android:inputType="numberDecimal":带小数点的浮点格式。

n android:inputType="phone":拨号键盘。

n android:inputType="datetime":时间日期。

n android:inputType="date":日期键盘。

n android:inputType="time":时间键盘。

EditText还派生了如下两个子类。

n AutoCompleteTextView:带有自动完成功能的EditText。由于该类通常需要与 Adapter结合使用,因此将会在下一章进行学习。

n ExtractEditText:并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

二、EditText示例

接下来通过一个简单的示例程序来学习EditText的常见用法。

同TextView示例程序一样,同样使用WidgetSample工程,在app/main/res/layout/目录下创建一个edittext_layout.xml文件,然后在其中填充如下代码片段:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:textSize="16sp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="16sp"/>
    <!-- android:inputType="numberPassword"表明只能接受数字密码 -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="numberPassword"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="年龄:"
        android:textSize="16sp"/>
    <!-- inputType="number"表明是数值输入框 -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        android:inputType="number"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="生日:"
        android:textSize="16sp"/>
    <!-- inputType="date"表明是日期输入框 -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入生日"
        android:inputType="date"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="电话号码:"
        android:textSize="16sp"/>
    <!-- inputType="phone"表明是输入电话号码的输入框 -->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入电话号码"
        android:inputType="phone"/>

</LinearLayout>

上面界面布局中的第一个文本框通过android:hint指定了文本框的提示信息:请输入用户名——这是该文本框默认的提示。当用户还没有输入时,该文本框内默认显示这段信息;

第二个输入框通过android:inputType="numberPassword”设置这是一个密码框,而且只能接受数字密码,用户在该文本框输入的字符会以点号代替;

第三个输入框通过android: inputType="number"设置为只能接受数值的输入框;

第四个输入框通过android:inputType= "date"指定它是一个日期输入框;

第五个输入框通过android:inputType= "phone”设置为一个电话号码输入框。

然后修改一下app/src/java/MainActivity.java文件中加载的布局文件为新建的edittext_layout.xml文件,修改后的代码如下:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

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

运行程序,可以看到下图所示界面效果。

EditText的示例程序就先到这里,关于其他使用方法建议大家自己进行练习。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-07-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 分享达人秀 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档