android中怎么将回车键变为确定按钮绑定回车键

android 应用监听输入法按键事件【比如搜索和回车键等】的整个流程分析_android开发_ThinkSAAS
android 应用监听输入法按键事件【比如搜索和回车键等】的整个流程分析
android 应用监听输入法按键事件【比如搜索和回车键等】的整个流程分析
继承于InputMethodService类的服务代码如下:
int keyCode = sKey.getKeyCode();KeyEvent eDown = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
keyCode, 0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD);
KeyEvent eUp = new KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode,
0, 0, 0, 0, KeyEvent.FLAG_SOFT_KEYBOARD);
onKeyDown(keyCode, eDown);
onKeyUp(keyCode, eUp);
上面的代码:把有关按键下发给应用,即应用监听输入法按键事件
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (processKey(event, 0 != event.getRepeatCount()))
return super.onKeyDown(keyCode, event);
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (processKey(event, true))
return super.onKeyUp(keyCode, event);
上面的down和up,我们重点看onKeyUp,跟踪下日志,是下面直接返回true了
if (processKey(event, true))
进入该方法,在该方法里面有如下代码为其覆盖代码:
if (processFunctionKeys(keyCode, realAction)) {
再进入该方法processFunctionKeys,跟踪其走入了下面代码:
if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (!realAction){
Log.d(TAG,"processFunctionKeys call KEYCODE_ENTER return");
sendKeyChar(&n&);
因为上面realAction为传进来的true,所以执行了如下
sendKeyChar(&n&);
进入该方法,该方法在InputMethodService中
public void sendKeyChar(char charCode) {
switch (charCode) {
case &n&: // Apps may be listening to an enter key to perform an action
if (!sendDefaultEditorAction(true)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
// Make sure that digits go through any text watcher on the client side.
if (charCode &= &0& && charCode
因为传进入的是&n&,所以执行了如下:
case &n&: // Apps may be listening to an enter key to perform an action
if (!sendDefaultEditorAction(true)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
看上面注释:// Apps may be listening to an enter key to perform an action,很清楚吧,呵呵,来看看这个方法吧
public boolean sendDefaultEditorAction(boolean fromEnterKey) {
EditorInfo ei = getCurrentInputEditorInfo();
if (ei != null &&
(!fromEnterKey || (ei.imeOptions &
EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(ei.imeOptions & EditorInfo.IME_MASK_ACTION) !=
EditorInfo.IME_ACTION_NONE) {
// If the enter key was pressed, and the editor has a default
// action associated with pressing enter, then send it that
// explicit action instead of the key event.
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
上面这个方法只是通知:让编辑器执行它,表示它可以做一个动作:
* Have the editor perform an action it has said it can do.
public boolean performEditorAction(int editorAction);
* Set of bits in
#imeOptions} that provide alternative actions
* associated with the "enter" key.
This both helps the IME provide
* better feedback about what the enter key will do, and also allows it
* to provide alternative mechanisms for providing that command.
public static final int IME_MASK_ACTION = 0x000000
我们来看这个方法EditableInputConnection中,有关为什么是EditableInputConnection看我之前的帖子就会明白了
public boolean performEditorAction(int actionCode) {
if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
mTextView.onEditorAction(actionCode);
一般应用程序想要监听回车或搜索按键则,如下写法:
edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
/*判断是否是“GO”键*/
if(actionId == EditorInfo.IME_ACTION_GO){
edittext.setText("success");
上面这个调用的是TextView中的接口
* Interface definition for a callback to be invoked when an action is
* performed on the editor.
public interface OnEditorActionListener {
* Called when an action is being performed.
* @param v The view that was clicked.
* @param actionId Identifier of the action.
This will be either the
* identifier you supplied, or
EditorInfo#IME_NULL
* EditorInfo.IME_NULL} if being called due to the enter key
* being pressed.
* @param event If triggered by an enter key,
* otherwise, this is null.
* @return Return true if you have consumed the action, else false.
boolean onEditorAction(TextView v, int actionId, KeyEvent event);
上面的又是怎么实现的呢?
* Set a special listener to be called when an action is performed
* on the text view.
This will be called when the enter key is pressed,
* or when an action supplied to the IME is selected by the user.
* this means that the normal hard key event will not insert a newline
* into the text view, even if it is multi- holding down the ALT
* modifier will, however, allow the user to insert a newline character.
public void setOnEditorActionListener(OnEditorActionListener l) {
if (mInputContentType == null) {
mInputContentType = new InputContentType();
mInputContentType.onEditorActionListener =
我们看到了OnEditorActionListener l赋值给了mInputContentType.onEditorActionListener
那么我们再回到上面EditableInputConnection中的方法:
public boolean performEditorAction(int actionCode) {
if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode);
mTextView.onEditorAction(actionCode);
进入如下方法:
mTextView.onEditorAction(actionCode);
public void onEditorAction(int actionCode) {
final InputContentType ict = mInputContentT
if (ict != null) {
if (ict.onEditorActionListener != null) {
if (ict.onEditorActionListener.onEditorAction(this,
actionCode, null)) {
看到了吧,之前放入mInputContentType的onEditorActionListener现在赋值给了InputContentType ict,然后执行了
ict.onEditorActionListener.onEditorAction(this,
actionCode, null)
现在我们明白了吧,搜索和回车等等按键就是这么实现回调的
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信实现android按下回车键便隐藏输入键盘,有两种方法:
1.)如果布局是多个EditText,为每个EditText控件设置android:singleLine=&true&,弹出的软盘输入法中回车键为next,直到最后一个获取焦点后显示为Done,点击Done后,软盘输入键盘便隐藏。或者将EditText的imeOptions属性设置android:imeOptions=&actionDone&,则不管是不是最后一个EditText,点击回车键即隐藏输入法。
2.)监听Enter的事件,编写Enter的事件响应。设置文本框的OnKeyListener,当keyCode ==KeyEvent.KEYCODE_ENTER的时候,表明Enter键被按下,就可以编写自己事件响应功能了。
具体代码:
package&listenter.&&
import&android.app.A&&
import&android.content.C&&
import&android.os.B&&
import&android.view.KeyE&&
import&android.view.V&&
import&android.view.View.OnKeyL&&
import&android.view.inputmethod.InputMethodM&&
import&android.widget.EditT&&
public&class&EnterListenter&extends&Activity&{&&
@Override&&
public&void&onCreate(Bundle&savedInstanceState)&{&&
super.onCreate(savedInstanceState);&&
setContentView(R.layout.main);&&
EditText&password=(EditText)findViewById(R.id.password);&&
password.setOnKeyListener(onKey);&&
OnKeyListener&onKey=new&OnKeyListener()&{&&
@Override&&
public&boolean&onKey(View&v,&int&keyCode,&KeyEvent&event)&{&&
if(keyCode&==&KeyEvent.KEYCODE_ENTER){&&
InputMethodManager&imm&=&(InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);&&
if(imm.isActive()){&&
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(),&0&);&&
图-1 点击回车键隐藏键盘
把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案actionNone : 回车键,按下后光标到下一行actionGo : Go,actionSearch : 一个放大镜actionSend : SendactionNext : NextactionDone : Done,隐藏软键盘,即使不是最后一个文本输入框
阅读(...) 评论()今天看啥 热点:
效果就是在EditView上输入内容后,可以直接点击键盘上的搜索键【由之前的回车键改编而来】
步骤如下:
1. 修改Editview属性:android:imeOptions=&actionSearch&&& 在该Editview获得焦点的时候将&回车&键改为&搜索&
&android:singleLine=&true&&&&& 不然回车【搜索】会换行
2.设置Editview的键盘监听
et_search_content.setOnKeyListener(new OnKeyListener() {//输入完后按键盘上的搜索键【回车键改为了搜索键】
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_ENTER){//修改回车键功能
// 先隐藏键盘
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(
FindProjectActivity.this
.getCurrentFocus()
.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
//跳转到搜索结果界面
b = new Bundle();
b.putString(&keyword&, &&.equals(et_search_content.getText().toString())?&0&:et_search_content.getText().toString());
b.putString(&city&, &&.equals(tv_province1.getText().toString())?&0&:tv_province1.getText().toString());
b.putString(&industry&, &&.equals(tv_hangye1.getText().toString())?&0&:tv_hangye1.getText().toString());
b.putString(&fund&, &&.equals(et_money.getText().toString())?&0&:et_money.getText().toString());
if(tv_time.getText().equals(&一周内&)){
b.putString(&time&, &w&);
if(tv_time.getText().equals(&一月内&)){
b.putString(&time&, &m&);
if(tv_time.getText().equals(&半年内&)){
b.putString(&time&, &hy&);
if(tv_time.getText().equals(&一年内&)){
b.putString(&time&, &y&);
Utils.startActivity(FindProjectActivity.this,
ProjectListActivity.class, b);
暂无相关文章
相关搜索:
相关阅读:
相关频道:
Android教程最近更新android中如何将回车键变为确定按钮?各种输入法 - Android当前位置:& &&&android中如何将回车键变为确定按钮?各种输入法android中如何将回车键变为确定按钮?各种输入法&&网友分享于:&&浏览:1次android中怎么将回车键变为确定按钮?各种输入法android:imeActionLabel="@string/sure"
android:imeOptions="actionDone"
android:inputType="text"&
这种方式在默认的输入法是可以实现的,但是如果换成第三方输入法就不行了,例如搜狗和百度请问怎么解决?
非常感谢&只有20分了------解决方案--------------------同问,我现在的搞法是自己设计了一个dialogfragment,放了个确定按钮在上面------解决方案--------------------涉及第三方输入法就不好搞了------解决方案--------------------没法弄,默认输入法可以,第三方我也不知道该怎么搞,而且android自带的action_done&都无法做到监听…
12345678910
12345678910
12345678910 上一篇:下一篇:文章评论相关解决方案 12345678910 Copyright & &&版权所有

我要回帖

更多关于 易语言按钮回车键 的文章

 

随机推荐