EditText
2014-01-09 09:54
260 查看
1.用EditText添加图片表情
package com.example.android_editview01; import java.lang.reflect.Field; import java.util.Random; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.text.Spannable; import android.text.SpannableString; import android.text.style.ImageSpan; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText=(EditText) findViewById(R.id.editText1); Button button=(Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub try { //通过反射机制取得图片id Field field=R.drawable.class.getDeclaredField("pic"+((new Random().nextInt(5))+1)); int resourceId=Integer.parseInt(field.get(null).toString()); Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resourceId); ImageSpan imageSpan=new ImageSpan(MainActivity.this,bitmap); //设置可拆分字符串 SpannableString spannableString=new SpannableString("face"); //用imageSpan代替字符串中[0-4)的字符 spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); editText.append(spannableString); } catch (Exception e) { // TODO: handle exception } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
2.用EditText做数据验证
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用android:digits属性验证数字" /> <EditText android:id="@+id/editText1" android:layout_width="200sp" android:layout_height="wrap_content" android:digits="0123456789" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用android:digits属性验证字母" /> <EditText android:id="@+id/editText2" android:layout_width="200sp" android:layout_height="wrap_content" android:digits="qwertyuiopasdfghjklzxcvbnm" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用android:inputType属性验证数字" /> <EditText android:id="@+id/editText3" android:layout_width="200sp" android:layout_height="wrap_content" android:inputType="number|textCapCharacters" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用android:inputType属性验证邮箱" /> <EditText android:id="@+id/editText4" android:layout_width="200sp" android:layout_height="wrap_content" android:inputType="textEmailAddress" /> <TextView android:id="@+id/textView5" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用android:numeric属性验证浮点数" /> <EditText android:id="@+id/editText5" android:layout_width="match_parent" android:layout_height="wrap_content" android:numeric="decimal" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击验证" /> </LinearLayout>
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String str=editText.getText().toString(); if(str==null||str.trim().equals("")){ editText.setError("请输入内容"); } } });
相关文章推荐
- android 代码实现控件之间的间距
- 解決Linux下Android开发真机调试设备不被识别问题
- [Android]在代码里运行另一个程序的方法
- [软件咨询]WPS2012正式版已发布 金山Office移动版4.0发布
- Android笔记-Linux Kernel Ftrace (Function Trace)解析
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- android USB如何修改VID具体实现
- Android Mouse实现过程详细笔记
- 深入Android Browser配置管理的详解
- Android Mms之:深入理解对话列表管理
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- UI Events 用户界面事件
- 基于jQuery UI CSS Framework开发Widget的经验
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
- jQuery UI的Dialog无法提交问题的解决方法
- 关于jQuery UI 使用心得及技巧
- jQuery ui 1.7更新小结
- Android APP与媒体存储服务的交互
- 从零开始学习jQuery (十) jQueryUI常用功能实战
- android 多线程技术应用