【小知识点总结】android:windowSoftInputMode属性详解 和 按back键直接退出Activity,不关闭软键盘的问题

来源:互联网 发布:手机炒股模拟软件 编辑:程序博客网 时间:2024/04/28 18:28

activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性。

这个属性能影响两件事情:

【一】当有焦点产生时,软键盘是隐藏还是显示

【二】是否减少活动主窗口大小以便腾出空间放软键盘

它的设置必须是下面列表中的一个值,或一个state…”值加一个adjust…”值的组合。在任一组设置多个值——多个state…”values,例如&mdash有未定义的结果。各个值之间用|分开。例如:<activity android:windowSoftInputMode="stateVisible|adjustResize". . . >

在这设置的值("stateUnspecified""adjustUnspecified"以外)将覆盖在主题中设置的值


各值的含义:

【A】stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置

【B】stateUnchanged:当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示

【C】stateHidden:用户选择activity时,软键盘总是被隐藏

【D】stateAlwaysHidden:当该Activity主窗口获取焦点时,软键盘也总是被隐藏的

【E】stateVisible:软键盘通常是可见的

【F】stateAlwaysVisible:用户选择activity时,软键盘总是显示的状态

【G】adjustUnspecified:默认设置,通常由系统自行决定是隐藏还是显示

【H】adjustResize:该Activity总是调整屏幕的大小以便留出软键盘的空间

【I】adjustPan:当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分


 

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


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

[java] view plaincopy
  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. }  

0 0
原创粉丝点击