0

我试图构建一个测试应用程序,它仅在发生配置更改时中继一条 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>

我的爪哇:

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

1 回答 1

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"

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

于 2012-06-29T17:24:12.053 回答