android系列2.EditText学习

只要介绍常用相关几个东东

1.默认是多行,单行文本 android:singleLine="true"

2.默认字符,android:hint="宝川"

3.图标显示,android:drawableLeft="@drawable/sina"

4.圆角,通过一个shape.xml文件设置

5.长度控制,android:maxLength="5"

6.输入控制,android:inputType=""这个里有多种选择,可以根据具体选择自己要的选项

效果如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="fill_parent"
 6     >
 7 <TextView  
 8     android:layout_width="fill_parent" 
 9     android:layout_height="wrap_content" 
10     android:text="@string/hello"
11     />
12 <!-- 设置这个为0dp,这样不显示,使显示出来的就失去焦点 -->
13 <EditText 
14     android:layout_width="0dp"
15     android:layout_height="0dp"
16 />
17 <EditText 
18     android:layout_width="fill_parent"
19     android:layout_height="wrap_content"
20     android:singleLine="true"
21     
22     android:inputType="textPassword"
23     android:hint="宝川"
24     android:drawableLeft="@drawable/sina"
25     android:background="@drawable/shape"
26     android:maxLength="5"
27 />
28 <!-- 
29     //单行文本
30     android:singleLine="true"
31     //多种选项,可以成密码输入,邮箱,数字等
32     android:inputType="textPassword"
33     //默认显示
34     android:hint="宝川"
35     //显示图片,可以在左右drawableRight上Top下Buttom显示
36     android:drawableLeft="@drawable/sina"
37     //圆角
38     android:background="@drawable/shape"
39  -->
40 </LinearLayout>

shape.xml文件

 1 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:shape="rectangle" android:padding="10dp">
 3     <!-- 填充颜色 -->
 4     <solid android:color="#FFFFFF" />
 5     
 6     <!-- corners 
 7         android:bottomRightRadius="15dp"
 8         android:bottomLeftRadius="15dp" 
 9         android:topLeftRadius="15dp"
10         android:topRightRadius="15dp"
11      /-->
12      <!-- 弧形半径 -->
13      <corners android:radius="15dip" />
14     
15 </shape>

上面要在res下drawable中添加图标和文件,R.java会自动生成两行代码

1 public static final class drawable {
2         public static final int icon=0x7f020000;
3         public static final int shape=0x7f020001;
4         public static final int sina=0x7f020002;
5     }
原文地址:https://www.cnblogs.com/atyou/p/2728170.html