EditText

作者: 大道至简峰 | 来源:发表于2015-11-13 11:26 被阅读123次

EditText是用来接收用户输入信息的,可以输入单行文本和多行文本;还可以通过在xml中指定android:inputType来指定EditText接受的输入类型(例如,密码,电话,邮件等)。通过指定了android:inputType后,输入的键盘也会对应改变,例如,如果指定android:inputType的值为电话类型,那么弹出来的输入法将会改变为只能输入数字的样式。

创建一个实例工程

先看看最后的成果!

最后的成果
  1. 打开Android Studio,选择File-->New-->New Moudule来创建一个Android应用。如下图:


    创建Android应用
  2. 选择第一个选项,然后单击next


  3. 填写应用名称(Application/Library name),项目名称(Module name)和包名(Package name),然后单击Next。


    Paste_Image.png
  4. 选择默认,然后单击Next。


    选择默认的空白Activity(Blank Activity)
  5. 单击Finish完成创建


    完成创建
  6. 创建完成后


    Paste_Image.png

注意:
这是最新版本的Android Studio,默认的布局文件有两个,activity_main.xml是主Activity的布局,content_main.xml是主布局的一个部分,被使用include包含进了activity_main.xml中。如下图:


我们暂时不用它提供的布局,把content_main.xml删了,然后把activity_main.xml中的内容删改成如下的样子:
<?xml version="1.0" encoding="utf-8"?>
<!--
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns(xml name space)。指定android所代表的名称空间为http://schemas.android.com/apk/res/android
    android:layout_width="match_parent"
        指定LinearLayout的宽度,match_parent意思是和它的父亲一样宽,这里它的父亲是整个屏幕,所以它的宽度是填充屏幕
    android:layout_height="match_parent"
        指定LinearLayout的高度,和整个屏幕一样高
    android:orientation="vertical"    
        指定LinearLayout中的子元素的排列方式是从上往下垂直排列
    android:padding="15dp"
        指定LinearLayout中的子元素距离LinearLayout的四条边(上下左右)的距离是15个dp
    >
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">



</LinearLayout>

Note
xml注释的格式是:

<!--注释的内容写在这里-->

写布局文件并运行

  1. 进行了上述步骤后,我们就可以开始写我们的应用的布局界面了。打开activity_main.xml,写入如下的代码:
<?xml version="1.0" encoding="utf-8"?>
    <!--
        xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns(xml name space)。指定android所代表的名称空间为http://schemas.android.com/apk/res/android
        android:layout_width="match_parent"
            指定LinearLayout的宽度,match_parent意思是和它的父亲一样宽,这里它的父亲是整个屏幕,所以它的宽度是填充屏幕
        android:layout_height="match_parent"
            指定LinearLayout的高度,和整个屏幕一样高
        android:orientation="vertical"
            指定LinearLayout中的子元素的排列方式是从上往下垂直排列
        android:padding="15dp"
            指定LinearLayout中的子元素距离LinearLayout的四条边(上下左右)的距离是15个dp
        >
    -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="15dp">

    <!--
        android:text
            显式在文本框上的内容
        android:layout_width="wrap_content"
            指定文本框的宽度为:和它其中的内容(即显式的文本)的宽度一样宽
        android:layout_height="wrap_content"
            指定文本框的高度为:和它其中的内容(即显式的文本)的高度一样高
    -->
    <TextView
        android:text="用户名:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--
        android:inputType="text"
            指定输入框接受的输入类型为:文本类型
        android:layout_width="match_parent"
            指定输入框的宽度:和它的父亲一样宽(它的父亲是LinearLayout,也就是和LinearLayout一样宽)
    -->
    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="密码:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--
       android:inputType="textPassword"
            指定输入框接受的输入类型是:文本密码
   -->
    <EditText
        android:inputType="textPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="电话:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--
      android:inputType="phone"
           指定输入框接受的输入类型是:电话号码
    -->
    <EditText
        android:inputType="phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="Email:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--
      android:inputType="textEmailAddress"
           指定输入框接受的输入类型是:邮箱
    -->
    <EditText
        android:inputType="textEmailAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
  1. 接下来打开MainActivity,修改为如下图所示的样子:


    Paste_Image.png
  2. 接下来就可以插上手机,运行程序了。(_)(你的手机必须开启USB调试,并且电脑安装了相应的驱动。至于怎么弄,可以百度,或者下次我再写
    点这个小三角形运行
  3. 运行后,点箭头1处的安卓监视器(Android Monitor),然后点箭头2所示的按钮可以录制3分钟的视频,点箭头2所示的按钮可以截屏。


    Paste_Image.png
  4. 到此为止,完成了!开心ing!,下面来看看我录制的动画(不能上传视频,只有转换为gif了)。
效果演示

相关文章

网友评论

      本文标题:EditText

      本文链接:https://www.haomeiwen.com/subject/dxzhhttx.html