Android中EditText控件的几种高级用法

转自: http://blog.csdn.net/ahuier/article/details/8982516


EditText是除了TextView控件之外的属性,还可以实现输入文本内容。下面我们就举几个实例来练习一下这种控件的使用方法。

1. 案例一:像QQ一样输入表情图像

主要代码:

[java]  view plain copy
  1. /** Called when the activity is first created. */  
  2. @Override  
  3. public void onCreate(Bundle savedInstanceState) {  
  4.     super.onCreate(savedInstanceState);  
  5.     setContentView(R.layout.main);  
  6.     initComponent();  
  7.       
  8.     button.setOnClickListener(new View.OnClickListener() {  
  9.           
  10.         @Override  
  11.         public void onClick(View v) {  
  12.               
  13.             int randomId = 1 + new Random().nextInt(5); //取得随机数randomId 范围在[1,6)之间   
  14.             try {  
  15.                 //利用反射机制:根据随机产生的1至5的整数从R.drawable类中获得相应资源ID(静态变量)的Field对象  
  16.                 Field field = R.drawable.class.getDeclaredField("face" + randomId); //取得图片的名称+1个随机数  
  17.                 //获得资源ID的值,也就是静态变量的值  
  18.                 int resourceId = Integer.parseInt(field.get(null).toString());  
  19.                   
  20.                 /* 
  21.                  * 在android重要要显示图片信息,必须使用Bitmap位图的对象来装载。 
  22.                  * 查看Android 的BitmapFactory的API文档:Public Methods,这些方法描述了如何讲一些字符串,字节数组转化为字节对象 
  23.                  */  
  24.                 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);  
  25.                   
  26.                 // 要让图片替代指定的文字就要用ImageSpan   
  27.                 ImageSpan imageSpan = new ImageSpan(EditTextDemoActivity.this, bitmap);  
  28.                 // 创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像  
  29.                 SpannableString spannableString = new SpannableString("face");  
  30.                 // 用ImageSpan对象替换face  
  31.                 spannableString.setSpan(imageSpan, 04, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
  32.                 // 将随机获得的图像追加到EditText控件的最后  
  33.                 editText.append(spannableString);  
  34.                   
  35.             } catch (Exception e) {  
  36.                 // TODO: handle exception  
  37.             }  
  38.               
  39.         }  
  40.     });  
  41. }  

Demo执行结果:


2. 案例二:在EditText中输入特定的字符,实现校验功能。

主要代码:

[java]  view plain copy
  1. button.setOnClickListener(new View.OnClickListener() {  
  2.       
  3.     @Override  
  4.     public void onClick(View v) {  
  5.         String value = editText.getText().toString();  
  6.         //trim() 表示输入前后空格  
  7.         if(value == null || value.trim().equals("")){  
  8.             editText.setError("请输入内容!");  
  9.             return;  
  10.         }  
  11.     }  
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="使用android:digits属性(输入数字)" />  
  11.   
  12.     <EditText  
  13.         android:id="@+id/num"  
  14.         android:layout_width="200dp"  
  15.         android:layout_height="wrap_content"  
  16.         android:layout_margin="10dp"  
  17.         android:digits="0123456789" />  
  18.   
  19.     <TextView  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="使用android:digits属性(输入小写字母)" />  
  23.   
  24.     <EditText  
  25.         android:layout_width="200dp"  
  26.         android:layout_height="wrap_content"  
  27.         android:layout_margin="10dp"  
  28.         android:digits="abcdefghijklmnopqrstuvwxyz" />  
  29.   
  30.     <TextView  
  31.         android:layout_width="fill_parent"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="使用android:input属性(输入数字)" />  
  34.   
  35.     <EditText  
  36.         android:layout_width="200dp"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_margin="10dp"  
  39.         android:inputType="number|textCapCharacters" />  
  40.   
  41.     <TextView  
  42.         android:layout_width="fill_parent"  
  43.         android:layout_height="wrap_content"  
  44.         android:text="使用android:input属性(输入Email格式)" />  
  45.   
  46.     <EditText  
  47.         android:layout_width="200dp"  
  48.         android:layout_height="wrap_content"  
  49.         android:layout_margin="10dp"  
  50.         android:inputType="textEmailAddress" />  
  51.   
  52.     <TextView  
  53.         android:layout_width="fill_parent"  
  54.         android:layout_height="wrap_content"  
  55.         android:text="使用android:input属性(输入带有符号的浮点数)" />  
  56.   
  57.     <EditText  
  58.         android:layout_width="200dp"  
  59.         android:layout_height="wrap_content"  
  60.         android:layout_margin="10dp"  
  61.         android:numeric="decimal|signed" />  
  62.   
  63.     <Button  
  64.         android:id="@+id/button"  
  65.         android:layout_width="fill_parent"  
  66.         android:layout_height="wrap_content"  
  67.         android:text="确认" />  
  68.   
  69. </LinearLayout>  
Demo执行结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值