2012-10-31 110 views
5

我正在製作android phonegap應用程序。
我在CordovaWebView下設置了editText。我想獲得鍵盤顯示/隱藏事件。
嘗試計算視圖高度,但失敗。當editText有焦點時,顯示鍵盤。但CordovaWebView上升,並且視圖大小不會改變。所以我無法獲得鍵盤顯示的事件。

Android中的Softkeyboard事件監聽器

爲什麼視圖上升?

這裏是我的部分代碼。

MainActivity onCreateMethod()

int layoutId = R.layout.blank; 
layout = new LinearLayout(this); 
setContentView(layoutId); 
layout.setOrientation(LinearLayout.VERTICAL); 

textedit = ((Activity) this).getLayoutInflater().inflate(R.layout.main,null); 

layout.addView((View) appView.getParent()); 
layout.addView(textedit); 

layout.getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1)); 
setContentView(layout); 

RES /佈局/ blank.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
</LinearLayout> 

RES /佈局/ main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <EditText 
    android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
</LinearLayout> 

請幫助....

回答

3

您是否試過傾聽「showkeyboard」和「hidekeyboard」事件?每次軟鍵盤顯示/隱藏時都應該觸發它們。

document.addEventListener("showkeyboard", function() { 
    console.log("Yay the keyboard is here"); 
}, false); 
document.addEventListener("hidekeyboard", function() { 
    console.log("Boo the keyboard is gone"); 
}, false); 
+0

謝謝!我努力了。在CordovaWebView中這些事件被正確觸發,但是當editText失去焦點時,showkeyboard事件被觸發...並且帶有「this.getWindow().setSoftInputMode(LayoutParams.WRAP_CONTENT);」showText在editText顯示時觸發了... – Yajap

+0

被更改爲「this 。.getWindow()setSoftInputMode(LayoutParams.WRAP_CONTENT);」到「this.getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);」,事件發生!!!!!謝謝!!! – Yajap

+0

嗨西蒙,這個事件不是在iOS設備上被解僱,是否有解決方法。 –