安卓系统:如何跟踪软键盘是否打开



在我的应用程序中,设备软键盘覆盖了EditTextViews,所以用户看不到他在键入什么。

所以我想隐藏其他视图(就像我的例子中的按钮),以使EditTextViews可见。

但相反,做一些类似的事情

EditText.onFocus(){
//hide button
}

对于每个EditText,我都想做一些类似的事情

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

如何跟踪软键盘是打开还是关闭?

编辑:

实际上我的布局是这样的:

   <?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>

当我在ScrollViewEditTextViews中键入文本时,问题就出现了

没有直接的方法可以找到它,但我提出了一个肮脏的解决方案,它运行良好。背后的想法是比较你的活动和键盘两个视图的高度。

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;
                }
            });
}

您可能应该在清单文件中使用android:windowSoftInputMode属性来描述当软键盘出现时屏幕布局应该如何排列(http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft)

  • android:windowSoftInputMode="adjustResize"

活动的主窗口总是调整大小,以便为软窗口腾出空间屏幕上的键盘。

  • android:windowSoftInputMode="adjustPan"

活动的主窗口没有调整大小以为软键盘腾出空间。相反,窗口的内容会自动平移,这样当前的焦点就不会被键盘遮挡,用户也可以随时看到他们在键入什么。这通常比调整大小更不可取,因为用户可能需要关闭软键盘才能进入窗口的遮挡部分并与之交互。

要处理软键盘检测,请在AndroidManifest.xml文件中设置活动的android:ConfigChanges属性:

<activity android:name="com.myapp.MyActivity"
          android:label="@string/app_name"
          android:configChanges="keyboard|keyboardHidden|orientation"
          >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

这里,android:configChanges中的keyboard|keyboardHidden标志告诉系统"活动"希望调用"活动"的onConfigurationChanged()方法,该方法允许您处理softkebaord更改。

下面是一个示例onConfigurationChanged(),它替换了当前运行的Activity:的主要内容视图

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Set a new layout - framework will automatically choose portrait or landscape as needed
    setContentView( R.layout.my_activity_layout );
    // Reconnect all listeners for controls in the layout
    a.findViewById( R.id.exit_button ).setOnClickListener( mListener );
}

很可能,您的onConfigurationChanged()方法需要做与"活动"的onCreate()方法几乎完全相同的工作,因此,通过将布局设置分解为一个单独的助手方法(从onCreate()onConfigurationChanged()调用)来DRY代码可能是个好主意。

最新更新