android基本控件EditText

来源:互联网 发布:物联网域名 编辑:程序博客网 时间:2024/05/21 21:13

根据老罗视频学习总结。


在TextView项目的基础上增加Menu按钮“EditText练习”选项,点击后弹出新的EditDemoActivity,在新的Activity里做EditText练习。

一.在EditText添加表情图片。

EditDemoActivity关联的布局文件是editdemoactivity.xml文件,放置一个EditText控件和一个Button控件。

如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/editText1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textSize="30sp"        android:ems="10" >        <requestFocus />    </EditText>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"//按钮置于布局中间        android:textSize="30sp"        android:text="添加QQ表情" /></LinearLayout>

EditDemoActivity.java内容如下:

@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//设置关联布局文件setContentView(R.layout.editdemoactivity);editText = (EditText)findViewById(R.id.editText1);button = (Button)findViewById(R.id.button1);//设置button控件的点击事件button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubint randId = 1+ new Random().nextInt(9);//随机数是从0开始产生的,所以要加1,0到8的随机数try {//获取这个图像的域Field field = R.drawable.class.getDeclaredField("face" + randId);//获取资源IDint resourceId = Integer.parseInt(field.get(null).toString());//在Android中要显示图片信息,必须使用Bitmap位图对象来加载Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resourceId);//用图片代替文字ImageSpan imageSpan = new ImageSpan(EditDemoActivity.this, bitmap);//SpannableString spannableString = new SpannableString("face");spannableString.setSpan(imageSpan, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);editText.append(spannableString);} catch (NoSuchFieldException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (NumberFormatException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalAccessException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}

效果图如下,点击按钮,会随机在EditText中加载图片:






二.在EditText控件输入特定的字符,并实现校验功能。


在布局文件中,通过属性android:digits;或者android:inputType;来规范输入EditText的格式。

<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"        android:textSize="15sp" android:text="使用android:digits属性(输入数字)"></TextView>    <EditText android:id="@+id/editText2"        android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_margin="10dp" android:digits="0123456789"></EditText>//规范只能输入0-9的数字        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"        android:textSize="15sp" android:text="使用android:digits属性(输入26个小写字母)"></TextView>    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_margin="10dp" android:digits="qwertyuiopasdfghjklzxcvbnm"></EditText>//规范只能输入小写字母        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"        android:textSize="15sp" android:text="使用android:inputType属性(输入数字)"></TextView>    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_margin="10dp" android:inputType="number|textCapCharacters"></EditText>//只能输入数字        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"        android:textSize="15sp" android:text="使用android:inputType属性(输入Email)"></TextView>    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_margin="10dp" android:inputType="textEmailAddress"></EditText>//输入邮件        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"        android:textSize="15sp" android:text="使用android:numeric属性(输入有符号的浮点数)"></TextView>    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"        android:layout_margin="10dp" android:numeric="decimal|signed"></EditText>//输入浮点数        <Button android:id="@+id/button2" android:layout_width="wrap_content"        android:layout_height="wrap_content" android:layout_gravity="center"        android:background="#ffff00"        android:text="确认"></Button>

android:digitis限制只能输入指定的字符;

android:inputType类型见这个链接:http://blog.csdn.net/chaod5659/article/details/17117193

就通过属性来设置输入的字符类型。


java中内容如下:

button2 = (Button)findViewById(R.id.button2);editText2 = (EditText)findViewById(R.id.editText2);button2.setOnClickListener(new View.OnClickListener() {//设置监听器@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubString str = editText2.getText().toString();//trim函数,移除字符串两边的空白字符或者其他预定义字符if(str==null||str.trim().equals("")){editText2.setError("请输入内容");}}});

点击按钮后,如果第一个EditText框为空的,那么会弹出提示框。

效果图如下:



三.使用AutoCompleteTextView控件和MuilAutoCompleteTextView控件。


<TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="AutoCompleteTextView" /><AutoCompleteTextView android:id="@+id/autotext"     android:layout_width="fill_parent" android:layout_height="wrap_content"/><TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="MultiAutoCompleteTextView" /><MultiAutoCompleteTextView android:id="@+id/multext"    android:layout_width="fill_parent" android:layout_height="wrap_content"/>

autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autotext);String[] autoStrings = new String[]{"联合国","联合国安理会","联合国五个常任理会","Google","Google Map"};//第二个参数表示适配器的下拉风格ArrayAdapter<String> adapter = new ArrayAdapter<String>(AutoCompleteActivity.this, android.R.layout.simple_dropdown_item_1line,autoStrings);autoCompleteTextView.setAdapter(adapter);multiAutoCompleteTextView = (MultiAutoCompleteTextView)findViewById(R.id.multext);multiAutoCompleteTextView.setAdapter(adapter);//CommaTokenizer内部类multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());


效果:







第二个逗号必须是英文逗号,否则不能实现多个自动补全














0 0
原创粉丝点击