您的位置:首页 > 产品设计 > UI/UE

android-UI组件实例大全(二)------EditText编辑框

2015-12-04 19:00 513 查看
EditText与前面第一个UI组件TextView非常相似,与Textview的最大区别在于:前者可以接受用户输入

好的话不多说,上实例:

EditText选择框: 

1.为我们的edit设置一个默认的提示文本:

代码:

[html] view
plaincopyprint?

<span style="font-family:Comic Sans MS">    <!-- 为editText添加默认提示文本 -->  

    <TableRow>  

        <TextView  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="用户名"  

        android:textSize="10pt" />  

  

        <EditText  

        android:id="@+id/editname"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:hint="请输入用户名"  

        android:selectAllOnFocus="true" />  

    </TableRow></span>  

运行截图:



代码解释:
hint:制定了文本框提示信息的内容
selectAllOnFocus:若文本时可以编辑的,当该UI组件获得焦点以后不是将光标移到文本的开始或者结尾位置,而是获取组件内所有文本的内容
效果如下:



2.将editText设为密码框:

代码:

[html] view
plaincopyprint?

<span style="font-family:Comic Sans MS"><!-- 为editText设置输入限制,这里是设为密码框 -->  

  

    <TextView  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="密码"  

        android:textSize="10pt" />  

  

    <EditText  

        android:id="@+id/editpassword"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:inputType="textPassword" /></span>  

运行截图:



代码解释:
inputType:指定文本框的类型,这里指定为密码框,输入后会变成...,当然也可以设为:
number:只接受数字输入
date:日期选输入框
phone:电话号码等
详细见一下链接:
http://blog.csdn.net/coder_pig/article/details/15505691

3.设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f

代码:

[html] view
plaincopyprint?

<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->  

  

    <TextView  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="备注"  

        android:textSize="10pt" />  

  

    <EditText  

        android:id="@+id/editpage"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:capitalize="words"  

          

        android:textScaleX="1.5"  

        android:minLines="2"  

        android:maxLines="3"  

   />  

运行截图:



代码解释:
capitalize = "words"这里的话设置只能输入字母,且首字母会大写
textScaleX = 设置字与字之间的水平间距,默认1.0f
minLines:设置未输入东西时的显示行数
maxLines:设置编辑框的最大行数,超过以后向下滚动;也可使用android:lines设置,效果一样

4.控制组件四周的间隔距离与内部文字与边间的距离

代码:

[html] view
plaincopyprint?

<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->  

  

     

   <EditText  

       android:layout_width="match_parent"  

       android:layout_height="wrap_content"  

       android:text="作为对比的EditText"  

   />  

     

   <EditText  

       android:layout_width="match_parent"  

       android:layout_height="wrap_content"  

       android:text="设置padding = 20dp"  

       android:padding="20dp"  

   />  

     

   <EditText  

       android:layout_width="match_parent"  

       android:layout_height="wrap_content"  

       android:padding="20dp"  

       android:text="设置margin = 20dp"  

       android:layout_margin="20dp"  

   />  

     

   <EditText  

       android:layout_width="match_parent"  

       android:layout_height="wrap_content"  

       android:text="只设置左右的margin = 30dp"  

       android:layout_marginLeft="30dp"  

       android:layout_marginRight="30dp"  

   />  

运行截图:



代码解释:

我们使用margin相关属性增加组件与外框的距离
使用padding增加组件内文字和组件边框的距离
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: