Android之EditText

来源:互联网 发布:vmware无网络访问权限 编辑:程序博客网 时间:2024/05/04 20:49

恰逢暑假,把有关Android的EditText相关内容整理一下:

一、为EditText添加图片

        在EditText中添加图片,只需要设置android:drawableLeft、android:drawableRight、android:drawableTop或者android:drawableBottom属性,在xml中即可完成。例如:

     

[html] view plaincopyprint?
  1. <span style="font-size:16px;">    <EditText  
  2.         android:id="@+id/text"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:hint="hello"  
  6.         android:drawableLeft="@drawable/search"  
  7.         /></span>  


效果如下:

 

二、为EditText添加Button

        在EditText中添加Button相对麻烦一些,这里实现的方法是利用相对布局RelativeLayout,关键在于设置android:layout_alignBaseline(alignRight、alignLeft、alignTop、alignBottom)属性使Button控件的边缘与EditText的边缘对齐。

       

[html] view plaincopyprint?
  1. <span style="font-size:18px;">    <RelativeLayout   
  2.         android:layout_width="fill_parent"   
  3.         android:layout_height="wrap_content"   
  4.         >   
  5.         <EditText   
  6.             android:id="@+id/searcheidt"   
  7.             android:layout_width="fill_parent"   
  8.             android:layout_height="wrap_content"   
  9.             android:singleLine="true"   
  10.             android:hint="search"   
  11.             />   
  12.         <Button   
  13.             android:id="@+id/search_btn"   
  14.             android:layout_width="wrap_content"   
  15.             android:layout_height="wrap_content"   
  16.             android:background="@drawable/search"  
  17.             android:layout_alignTop="@id/searcheidt"   
  18.             android:layout_alignRight="@id/searcheidt"   
  19.             android:layout_marginRight="5px"   
  20.             />   
  21.       </RelativeLayout> </span>  


这里我设置了android:layout_alignTop和 android:layout_alignRight属性,是Button的顶部边缘和右边缘分别于EditText的顶部边缘和右边缘对齐,即可实现(此时搜索图标表示一个按钮,通过设置监听器可以实现相应功能):

 

三、shape的使用

       在android中常常用shape来修改控件的显示属性,比如:圆角、描边之类的。首先,写一个xml文件:edittext_shape.xml

         

[html] view plaincopyprint?
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <!--圆角-->  
  4.     <corners  
  5.         android:radius="5dp"/>  
  6.     <!--实心-->  
  7.     <solid  
  8.         android:color="#eeeeee"/>  
  9.     <!--描边-->  
  10.     <stroke  
  11.         android:width="1dip"  
  12.         android:color="#c6cbce"/>  
  13.     <!--渐变-->  
  14.     <gradient/>  
  15. </shape></span>  


把以上edittext_shape.xml文件放到drawable文件夹内,然后只需要在设置EditText的background属性即可:

 android:background="@drawable/edittext_shape"

 

如下:

           

[html] view plaincopyprint?
  1. <span style="font-size:18px;"><EditText  
  2.       android:id="@+id/text"  
  3.       android:layout_width="fill_parent"  
  4.       android:layout_height="wrap_content"  
  5.       android:hint="hello"  
  6.       android:drawableLeft="@drawable/search"  
  7.       android:background="@drawable/edittext_shape"  
  8.       /></span>  

效果如下:


  
就先整理这些。。。


原文地址:http://blog.csdn.net/ma12an/article/details/7758464