原文地址:http://blog.csdn.net/wang_shaner/article/details/8467688
"EditText + Button" 形成一个 "输入+按键响应" 的案例在android编程中是最常见不过的了。
但还有一些细节需要注意:
- 在EditText输入后,点击Button进行请求,软键盘应该自行消失
- 在EditText输入后,不点击Button进行请求,而是直接点击软键盘上的"回车",那么也应该能够正常响应请求
- InputMethodManagerimm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
- imm.hideSoftInputFromWindow(mEditText.getWindowToken(),0);
- publicvoidsetOnEditorActionListener(TextView.OnEditorActionListenerl)
- Setaspeciallistenertobecalledwhenanactionisperformedonthetextview.Thiswillbecalledwhentheenterkeyispressed,orwhenanactionsuppliedtotheIMEisselectedbytheuser.Settingthismeansthatthenormalhardkeyeventwillnotinsertanewlineintothetextview,evenifitismulti-line;holdingdowntheALTmodifierwill,however,allowtheusertoinsertanewlinecharacter.
- mEditText.setOnEditorActionListener(newTextView.OnEditorActionListener(){
- @Override
- publicbooleanonEditorAction(TextViewv,intactionId,KeyEventevent){
- //TODO这里做"回车"响应处理
- returntrue;
- }
- });
IME_ACTION_DONE |
IME_ACTION_GO |
IME_ACTION_NEXT |
IME_ACTION_NONE |
IME_ACTION_PREVIOUS |
IME_ACTION_SEARCH |
IME_ACTION_SEND |
IME_ACTION_UNSPECIFIED |
软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
- actionUnspecified 未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
- actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
- actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
- actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果:
- actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
- actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
- actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:
下面已搜索为例,演示一个实例,修改main.xml如下:
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns: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如下:
- packagecom.flysnow;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.KeyEvent;
- importandroid.widget.EditText;
- importandroid.widget.TextView;
- importandroid.widget.Toast;
- importandroid.widget.TextView.OnEditorActionListener;
- publicclassHelloEditTextextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- EditTexteditText=(EditText)findViewById(R.id.edit_text);
- editText.setOnEditorActionListener(newOnEditorActionListener(){
- @Override
- publicbooleanonEditorAction(TextViewv,intactionId,KeyEventevent){
- Toast.makeText(HelloEditText.this,String.valueOf(actionId),Toast.LENGTH_SHORT).show();
- returnfalse;
- }
- });
- }
- }
运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。