按back键直接退出Activity,不关闭软键盘

时间:2023-01-11 21:29:02

转自:http://blog.csdn.net/twoicewoo/article/details/7385876

====================================================================

自定义一个layout,覆写dispatchKeyEventPreIme(KeyEvent event)方法,请看QuickSearchBox的源码

 1 /*
 2  * Copyright (C) 2010 The Android Open Source Project
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.quicksearchbox.ui;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.KeyEvent;
23 import android.view.inputmethod.InputMethodManager;
24 import android.widget.RelativeLayout;
25 
26 /**
27  * Finishes the containing activity on BACK, even if input method is showing.
28  */
29 public class SearchActivityView extends RelativeLayout {
30 
31     public SearchActivityView(Context context) {
32         super(context);
33     }
34 
35     public SearchActivityView(Context context, AttributeSet attrs) {
36         super(context, attrs);
37     }
38 
39     public SearchActivityView(Context context, AttributeSet attrs, int defStyle) {
40         super(context, attrs, defStyle);
41     }
42 
43     private Activity getActivity() {
44         Context context = getContext();
45         if (context instanceof Activity) {
46             return (Activity) context;
47         } else {
48             return null;
49         }
50     }
51 
52     /**
53      * Hides the input method.
54      */
55     protected void hideInputMethod() {
56         InputMethodManager imm = (InputMethodManager)
57                 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
58         if (imm != null) {
59             imm.hideSoftInputFromWindow(getWindowToken(), 0);
60         }
61     }
62 
63     /**
64      * Overrides the handling of the back key to dismiss the activity.
65      */
66     @Override
67     public boolean dispatchKeyEventPreIme(KeyEvent event) {
68         Activity activity = getActivity();
69         if (activity != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
70             KeyEvent.DispatcherState state = getKeyDispatcherState();
71             if (state != null) {
72                 if (event.getAction() == KeyEvent.ACTION_DOWN
73                         && event.getRepeatCount() == 0) {
74                     state.startTracking(event, this);
75                     return true;
76                 } else if (event.getAction() == KeyEvent.ACTION_UP
77                         && !event.isCanceled() && state.isTracking(event)) {
78                     hideInputMethod();
79                     activity.onBackPressed();
80                     return true;
81                 }
82             }
83         }
84         return super.dispatchKeyEventPreIme(event);
85     }
86 }