歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 判斷軟鍵盤的狀態(顯示,隱藏)

Android 判斷軟鍵盤的狀態(顯示,隱藏)

日期:2017/3/1 10:18:30   编辑:Linux編程

先上截圖,有圖有真相:



自定義RelativeLayout

  1. package com.demo.softkeyboard;
  2. import Android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.widget.RelativeLayout;
  5. public class KeyboardListenRelativeLayout extends RelativeLayout {
  6. private static final String TAG = KeyboardListenRelativeLayout.class.getSimpleName();
  7. public static final byte KEYBOARD_STATE_SHOW = -3;
  8. public static final byte KEYBOARD_STATE_HIDE = -2;
  9. public static final byte KEYBOARD_STATE_INIT = -1;
  10. private boolean mHasInit = false;
  11. private boolean mHasKeyboard = false;
  12. private int mHeight;
  13. private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;
  14. public KeyboardListenRelativeLayout(Context context) {
  15. super(context);
  16. }
  17. public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
  18. super(context, attrs);
  19. }
  20. public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
  21. super(context, attrs, defStyle);
  22. }
  23. public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
  24. this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
  25. }
  26. @Override
  27. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  28. super.onLayout(changed, l, t, r, b);
  29. if(!mHasInit) {
  30. mHasInit = true;
  31. mHeight = b;
  32. if(onKeyboardStateChangedListener != null) {
  33. onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);
  34. }
  35. } else {
  36. mHeight = mHeight < b ? b : mHeight;
  37. }
  38. if(mHasInit && mHeight > b) {
  39. mHasKeyboard = true;
  40. if(onKeyboardStateChangedListener != null) {
  41. onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
  42. }
  43. }
  44. if(mHasInit && mHasKeyboard && mHeight == b) {
  45. mHasKeyboard = false;
  46. if(onKeyboardStateChangedListener != null) {
  47. onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
  48. }
  49. }
  50. }
  51. public interface IOnKeyboardStateChangedListener {
  52. public void onKeyboardStateChanged(int state);
  53. }
  54. }
用法:
  1. package com.demo.softkeyboard;
  2. import com.demo.softkeyboard.KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. /**
  9. * 軟鍵盤監聽Demo
  10. * @author qiaoning
  11. *
  12. */
  13. public class SoftKeyboardListenDemoActivity extends Activity {
  14. private EditText editText;
  15. KeyboardListenRelativeLayout relativeLayout;
  16. @Override
  17. public void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.main);
  20. relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);
  21. editText = (EditText) findViewById(R.id.editText);
  22. relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {
  23. public void onKeyboardStateChanged(int state) {
  24. switch (state) {
  25. case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://軟鍵盤隱藏
  26. editText.setVisibility(View.VISIBLE);
  27. break;
  28. case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://軟鍵盤顯示
  29. editText.setVisibility(View.GONE);
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. });
  36. }
  37. }
使用到的布局文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.demo.softkeyboard.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/keyboardRelativeLayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <ScrollView android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:layout_alignParentLeft="true"
  9. android:layout_alignParentTop="true"
  10. android:fillViewport="true">
  11. <LinearLayout android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:orientation="vertical">
  14. <EditText android:id="@+id/editText"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"/>
  17. </LinearLayout>
  18. </ScrollView>
  19. </com.demo.softkeyboard.KeyboardListenRelativeLayout>

源碼下載地址

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2012年資料/6月/30日/Android 判斷軟鍵盤的狀態(顯示,隱藏)

Copyright © Linux教程網 All Rights Reserved