当前位置: 代码迷 >> Android >> android EditText运用详解
  详细解决方案

android EditText运用详解

热度:40   发布时间:2016-05-01 16:21:25.0
android EditText使用详解

五:为文本指定特定的软键盘类型

前面我们通过指定为电话号码特定格式,然后键盘类型变成了拨号专用的键盘,这个是自动变的,其实我们也可以通 过android:inputType来设置文本的类型,让输入法选择合适的软键盘的。。android:inputType有很多类型,这里使用date类型来演示,修改main.xml如下:

Xml代码 复制代码?
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><EditText	android:id="@+id/edit_text"      android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:inputType="date"/></LinearLayout>

? 运行效果如下:

六:Enter键图标的设置

软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

  1. actionUnspecified? 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
  2. actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
  3. actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
  4. actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果:
  5. actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
  6. actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
  7. actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:

?下面已搜索为例,演示一个实例,修改main.xml如下:

Xml代码 复制代码??
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><EditText	android:id="@+id/edit_text"      android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:imeOptions="actionSearch"/></LinearLayout>

? 修改HelloEditText如下:

Java代码 复制代码?
package com.flysnow;import android.app.Activity;import android.os.Bundle;import android.view.KeyEvent;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import android.widget.TextView.OnEditorActionListener;public class HelloEditText extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        EditText editText=(EditText)findViewById(R.id.edit_text);        editText.setOnEditorActionListener(new OnEditorActionListener() {			@Override			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {				Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();				return false;			}		});    }}

?运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。

?

?

?

?

?

?

?

?

?

本文版权归飞雪无情 所有,转载请注明出处, 永久链接: http://flysnow.iteye.com/blog/828415

?

  相关解决方案