【问题标题】:Softkeyboard event Listener in AndroidAndroid中的软键盘事件监听器
【发布时间】:2014-01-15 22:09:40
【问题描述】:

我正在制作 android phonegap 应用程序。
我在 CordovaWebView 下设置了 editText。我想获得键盘显示/隐藏事件。
尝试计算视图高度,但失败。当 editText 具有焦点时,将显示键盘。但是 CordovaWebView 上升了,并且视图大小没有改变。所以我无法获得键盘显示事件。

为什么浏览量会上升?

这是我的部分代码。

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/layout/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/layout/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>

请帮忙....

【问题讨论】:

    标签: android cordova keyboard


    【解决方案1】:

    您是否尝试过监听“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);
    

    【讨论】:

    • 谢谢!我努力了。在 CordovaWebView 中,这些事件正确触发,但是当 editText 失去焦点时触发 showkeyboard 事件...并且使用“this.getWindow().setSoftInputMode(LayoutParams.WRAP_CONTENT);”在显示 editText 时触发 showkeyboard...
    • 已更改为“this.getWindow().setSoftInputMode(LayoutParams.WRAP_CONTENT);”到“this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);”,事件触发!!!!!!谢谢!!!
    • 嗨 Simon,iOS 设备上不会触发此事件,是否也有解决方法。
    • @VarunAgarwal 我不知道。
    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2014-10-08
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多