如何在我的一个标签中响应onConfigChanges?

时间:2011-07-29 03:58:51

标签: android tabs broadcastreceiver android-tabhost onconfigurationchanged

我在纵向模式下有标签布局。现在我必须回复我在其中一个标签中发生的onConfigChanges。在谷歌研究之后我制作了这段代码:

的manifest.xml

<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".aaa_bbb" android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:screenOrientation="portrait"
              android:windowSoftInputMode="stateUnchanged|adjustPan"
              android:configChanges="keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="aaa">
    </activity>
    <activity android:name="bbb">
      <intent-filter>
            <action android:name="com.mycompany.myApp.KEYBOARD" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

我的主要活动:

public class aaa_bbb extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, aaa.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("aaa");
    spec.setIndicator("Aaa",res.getDrawable(R.drawable.tab_aaa));
    spec.setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent(this, bbb.class); /* just different way to do the same job */
    spec = tabHost.newTabSpec("bbb");
    spec.setIndicator("Bbb", res.getDrawable(R.drawable.tab_bbb));
    spec.setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Toast.makeText(getBaseContext(), "Config changes", Toast.LENGTH_LONG).show();
    Intent i = new Intent();
    i.setAction("com.mycompany.myApp.KEYBOARD");
    sendBroadcast(i);
}
}

最后在我的标签类(活动)中:

public class bbb extends Activity {
private MyListener listener = null;
private Boolean listener_is_registered = false;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bbb);

    listener = new MyListener();
}

@Override
public void onResume() {
    super.onResume();

    if (!listener_is_registered) {
        registerReceiver(listener, new IntentFilter("com.mycompany.myApp.KEYBOARD"));
        listener_is_registered = true;
        Toast.makeText(getBaseContext(), "Listener registered true.", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onPause() {
    super.onPause();

    if (listener_is_registered) {
        unregisterReceiver(listener);
        listener_is_registered = false;
        Toast.makeText(getBaseContext(), "Listener registered false", Toast.LENGTH_LONG).show();
    }
}


// Nested 'listener'
protected class MyListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // No need to check for the action unless the listener will
        // will handle more than one - let's do it anyway
        if (intent.getAction().equals("com.mycompany.myApp.KEYBOARD")) {
            /* perform some action */
            Toast.makeText(getBaseContext(), "Intent reciewied", Toast.LENGTH_LONG).show();
        }
    }
}
}

我不知道我错在哪里,但我的bbb活动仍然没有得到键盘隐藏或显示的响应。

1 个答案:

答案 0 :(得分:1)

你没有正确地确定方向。你想在某个方向或隐藏键盘后做一些其他动作吗?像这样添加布尔值以帮助跟踪可能会有所帮助:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


        isHorizontal=true;

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        isVertical=true;
    }
  }