安卓:如何跟踪如果软键盘openned? [英] Android: How to track if soft-keyboard is openned?

查看:242
本文介绍了安卓:如何跟踪如果软键盘openned?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,设备软键盘覆盖EditTextViews,所以用户无法看到他是打字。

In my application, the device soft-keyboard is covering the EditTextViews, so user cannot see what he is typing.

所以,我想隐藏其他视图(如在我的情况下,一个按钮),使EditTextViews可见。

So i want to hide other Views(like a button in my case), to make the EditTextViews visible.

但在而不是做这样的事情。

But instead on doing something like

EditText.onFocus(){
//hide button
}

对于每个EditText上,我想要做类似

for every EditText, i want to do something like

if(soft-keyboard.isOpenned())
//hide button
}

如何跟踪如果软键盘openned还是关闭?

how to track if soft-keyboard is openned or closed?

编辑:

其实我的布局是这样的:

Actually my layout is this:

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/transparent"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    </LinearLayout>

    <ImageView
        android:id="@+id/Logo"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/wic_logo_small" />

    <Button
        android:id="@+id/goButton_iWant"
        android:layout_width="35dp"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:background="@color/black"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/go" />

    <AutoCompleteTextView
        android:id="@+id/searchAutoCompleteTextView_iWant"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/goButton_iWant"
        android:layout_toRightOf="@id/Logo"
        android:hint="@string/search"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/iWantLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/iWantPageLogo"
        android:background="@color/grey"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/iWant"
        android:textColor="@color/white" />

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ScrollViewiWant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/iWantLabel"
        android:scrollbars="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/iNeedToBuy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iWantLabel"
                android:gravity="center_vertical|center_horizontal"
                android:padding="5dp"
                android:text="@string/iNeedToBuy"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <AutoCompleteTextView
                android:id="@+id/iNeedToBuyEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iNeedToBuy"
                android:hint="@string/product"
                android:imeOptions="actionNext"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/iNeedToBuyEditText"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <TextView
                android:id="@+id/priceWillingToPay"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/error1"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/priceWillingToPay"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/priceWillingToPayEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/priceWillingToPay"
                android:hint="@string/price"
                android:inputType="number"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/priceWillingToPayEditText"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <TextView
                android:id="@+id/needToBuyItBy"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/error2"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/needToBuyItBy"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <EditText
                android:id="@+id/date_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/needToBuyItBy"
                android:clickable="true"
                android:cursorVisible="false"
                android:focusable="false"
                android:hint="@string/date"
                android:inputType="none"
                android:singleLine="true"
                android:textColor="@color/white"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/error3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/date_iWant"
                android:height="0dp"
                android:text=""
                android:textColor="@color/error_color"
                android:textSize="12dp" />

            <DatePicker
                android:id="@+id/datePicker_iWant"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_below="@id/error3"
                android:hint="@string/date"
                android:padding="5dp"
                android:textColor="@color/black"
                android:textSize="15dp" />

            <TextView
                android:id="@+id/iAmIn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/date_iWant"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/iAmIn"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <Spinner
                android:id="@+id/city_spinner_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/iAmIn"
                android:prompt="@string/citySpinner"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <Button
                android:id="@+id/submitButton_iWant"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/city_spinner_iWant"
                android:gravity="center_vertical|center_horizontal"
                android:text="@string/submit"
                android:textColor="@color/white"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/emptySpace"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_below="@id/submitButton_iWant"
                android:gravity="left" />
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/buttons_iWant"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/feedButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/feed"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/iWantButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/iwant"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/shareButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/share"
            android:textColor="@color/black" />

        <Button
            android:id="@+id/profileButton_iWant"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_margin="0dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:text="@string/profile"
            android:textColor="@color/black" />
    </LinearLayout>

</RelativeLayout>

当我打字文本,问题就来了 EditTextViews 滚动型

The problem comes when i am typing text in the EditTextViews in the ScrollView

推荐答案

有没有直接的方法找到它,但我想出了一个肮脏的解决方案,它做工精细。背后的想法是比较两种观点您的活动和键盘。高度

There is no direct way to find it, but i come up with a dirty solution and it work fine. The idea behind is to compare the height of two views your activity and keyboard.

boolean watcherState;
private void isShowing() {
    final RelativeLayout relative = (RelativeLayout) findViewById(R.id.parent);
    relative.getViewTreeObserver().addOnGlobalLayoutListener(
            new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    // r will be populated with the coordinates of your view
                    // that area still visible.
                    relative.getWindowVisibleDisplayFrame(r);

                    int heightDiff = relative.getRootView().getHeight()
                            - (r.bottom - r.top);
                    Log.d("Height", heightDiff + "//");
                    if (heightDiff > 100) { // if more than 100 pixels, its
                                            // probably a keyboard...
                        watcherState = true;
                    } else
                        watcherState = false;
                }
            });

}

这篇关于安卓:如何跟踪如果软键盘openned?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆