检测uiMode或键盘改变android

时间:2012-04-18 13:53:42

标签: android android-ui android-configchanges

我尝试构建一个测试应用程序,仅在配置发生更改时才转发toast消息。 (这就是它的假设,它不起作用)最终的目的是检测用户是否将平板电脑放入键盘支架或将其从一个键盘支架中移除。我的清单和主要活动都在下面..我认为这个代码会在平板电脑配置更改为uiMode或外部键盘时发出祝酒..但当我停靠/取消停靠时没有任何事情发生..请帮助

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="12" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".UiModeTestActivity"
        android:configChanges="keyboard|uiMode"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的java:

   package com.eliddell;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.Toast;

public class UiModeTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      Toast.makeText(getApplicationContext(), "new config:"+newConfig, Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错,但是......

正如消息来源所说:

/**
 * The kind of keyboard attached to the device.
 * One of: {@link #KEYBOARD_NOKEYS}, {@link #KEYBOARD_QWERTY},
 * {@link #KEYBOARD_12KEY}.
 */
public int keyboard;

所以,我认为如果KEYBOARD_QWERTY改为KEYBOARD_12KEY,键盘会改变

来源的另一部分说:

  /**
     * A flag indicating whether any keyboard is available.  Unlike
     * {@link #hardKeyboardHidden}, this also takes into account a soft
     * keyboard, so if the hard keyboard is hidden but there is soft
     * keyboard available, it will be set to NO.  Value is one of:
     * {@link #KEYBOARDHIDDEN_NO}, {@link #KEYBOARDHIDDEN_YES}.
     */
    public int keyboardHidden;

因此,您可以尝试android:configChanges="keyboard|uiMode"

而不是android:configChanges="keyboardHidden|uiMode"

不幸的是我没有适配器将键盘插入我的设备并检查我的理论。试试吧!