如何在我的选项卡之一中响应 onConfigChanges?

发布于 2024-11-27 06:58:39 字数 4332 浏览 7 评论 0原文

我有纵向模式的选项卡布局。现在我必须对我的选项卡之一中发生的 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活动仍然没有得到键盘隐藏或显示的响应。

I have tab layout in portrait mode. Now I have to respond for onConfigChanges happened in one of my tabs. Following research on google I made this code:

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" />

My main activity:

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

And finally in my tab class (activity):

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

I don't known where I did mistake, but my bbb activity still do not get response for keyboard hide or show.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不离久伴 2024-12-04 06:58:39

您没有正确确定方向。您想在进入某个方向后执行其他操作还是只是隐藏键盘?像这样添加布尔值来帮助跟踪可能会有所帮助:

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

Your not figuring out the orientation properly. Do you want to do some other action after going to a certain orientation or just hide keyboards? Something like this adding booleans to help keep track would probably help:

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;
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文