如何在我的选项卡之一中响应 onConfigChanges?
我有纵向模式的选项卡布局。现在我必须对我的选项卡之一中发生的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确确定方向。您想在进入某个方向后执行其他操作还是只是隐藏键盘?像这样添加布尔值来帮助跟踪可能会有所帮助:
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: