2012-09-08 193 views
0
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 


    // Checks whether a hardware keyboard is available 
    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) { 
     Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show(); 
    } else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) { 
     Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show(); 
    } 
} 

我想在運行時檢測鍵盤可見性。上面的代碼在android 2.2中不起作用。我需要一個鍵盤事件監聽器的解決方案。在manifest.xml中Android鍵盤問題

+0

此代碼不適用於鍵盤可見性 –

回答

0

我還添加configChanges屬性是Android的一個痛點。您需要使用自定義視圖作爲根視圖,並應用使用onSizeChanged()+ +的佈局更改的差異來調用自定義回調的偵聽器。

示例視圖。

/** 
* Subclass of RelativeLayout that adds a size changed listener. This is useful for determining 
* when the onscreen keyboard has popped up and resized the window 
* 
*/ 
public class SCRelativeLayout extends RelativeLayout { 
    public interface OnSizeChangedListener { 
    public void onSizeChanged(int w, int h, int oldw, int oldh); 
    } 

    private OnSizeChangedListener mOnSizeChangedListener; 

    public SCRelativeLayout(Context context) { 
    super(context); 
    } 

    public SCRelativeLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public SCRelativeLayout(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onSizeChanged (int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 

    if (mOnSizeChangedListener != null) { 
     mOnSizeChangedListener.onSizeChanged(w, h, oldw, oldh); 
    } 
    } 

    public void setOnSizeChangedListener(OnSizeChangedListener listener) { 
    mOnSizeChangedListener = listener; 
    } 
} 
+0

如何使它在我們的活動類中工作? – dilipkaklotar

+0

將此設置爲佈局中的根視圖,並在您的活動的onCreate中,使用定義的方法爲其設置偵聽器對象。它會在佈局更改時調用您的方法,並可以比較尺寸。 – tsmith