在 TabActivity 中切换到新选项卡时关闭 Android PopupWindow?
我有一个 MapActivity 作为 TabActivity 中的四个选项卡之一。这个MapActivity可以启动一个PopupWindow,这是一个传奇。弹出窗口保留在屏幕上的地图顶部,直到再次单击“显示图例”按钮(来回等)。
问题是,当用户切换到另一个选项卡时,PopupWindow 在视图上保持不变。
我尝试在 MapActivity 类中实现 onPause() 方法,然后从那里将其关闭。使用此方法后,应用力将关闭。
有什么帮助吗?谢谢!
编辑:这是我的一些代码:
在 MainActivity 中,它建立了四个选项卡:
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, FirstActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("game").setIndicator("First",
res.getDrawable(R.drawable.ic_tab_game))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("alerts").setIndicator("Second",
res.getDrawable(R.drawable.ic_tab_alert))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapActivity.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LastActivity.class);
spec = tabHost.newTabSpec("experience").setIndicator("Last",
res.getDrawable(R.drawable.ic_tab_experience))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
现在在我的 MapActivity 类(扩展 MapActivity)中:
// Declare the Legend PopupWindow
mapLegendInflater = (LayoutInflater) MapActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mapLegendPopupLayout = mapLegendInflater.inflate(
R.layout.maptablegendpopuplayout, null, false);
mapLegendPopup = new PopupWindow(mapLegendPopupLayout,
(int) (0.45 * getApplicationContext().getResources()
.getDisplayMetrics().widthPixels),
(int) (0.33 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels), true);
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);
Boolean legendIsShown = false;
mapLegendButton = (Button) findViewById(R.id.buttonMapLegend);
mapLegendButton.setOnClickListener(mapLegendListener);
private OnClickListener mapLegendListener = new OnClickListener() {
public void onClick(View v) {
// Launch or dismiss the map legend popup
if (legendIsShown) {
mapLegendPopup.dismiss();
mapLegendButton.getBackground().clearColorFilter();
legendIsShown = false;
} else {
mapLegendPopup.showAtLocation(
findViewById(R.id.buttonMapLegend), Gravity.TOP
| Gravity.LEFT, 8,
(int) (0.23 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels));
mapLegendButton.getBackground().setColorFilter(
new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
// mapLegendButton.getBackground().setColorFilter(0xFFFFFF00,
// PorterDuff.Mode.MULTIPLY);
legendIsShown = true;
}
}
};
我希望这可以让您了解我所在的位置。 “地图”选项卡上的一切都运行良好。仅当您显示图例并切换选项卡时,它仍会显示在其他视图上。
I have a MapActivity as one of four tabs in a TabActivity. This MapActivity can launch a PopupWindow that is a legend. The PopupWindow remains on the screen, on top of the map, until the "Show Legend" button is clicked again (back and forth, etc.).
The problem is that, when a user switches to another tab, the PopupWindow remains persistent over the view.
I've tried implementing the onPause() method in the MapActivity class, and dismissing it from there. The application force closes with this method in place.
Any help? Thanks!
EDIT: Here's some of my code:
In the MainActivity, which establishes the four tabs:
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, FirstActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("game").setIndicator("First",
res.getDrawable(R.drawable.ic_tab_game))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, SecondActivity.class);
spec = tabHost.newTabSpec("alerts").setIndicator("Second",
res.getDrawable(R.drawable.ic_tab_alert))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapActivity.class);
spec = tabHost.newTabSpec("map").setIndicator("Map",
res.getDrawable(R.drawable.ic_tab_map))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, LastActivity.class);
spec = tabHost.newTabSpec("experience").setIndicator("Last",
res.getDrawable(R.drawable.ic_tab_experience))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
Now in my MapActivity class (which extends MapActivity):
// Declare the Legend PopupWindow
mapLegendInflater = (LayoutInflater) MapActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mapLegendPopupLayout = mapLegendInflater.inflate(
R.layout.maptablegendpopuplayout, null, false);
mapLegendPopup = new PopupWindow(mapLegendPopupLayout,
(int) (0.45 * getApplicationContext().getResources()
.getDisplayMetrics().widthPixels),
(int) (0.33 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels), true);
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);
Boolean legendIsShown = false;
mapLegendButton = (Button) findViewById(R.id.buttonMapLegend);
mapLegendButton.setOnClickListener(mapLegendListener);
private OnClickListener mapLegendListener = new OnClickListener() {
public void onClick(View v) {
// Launch or dismiss the map legend popup
if (legendIsShown) {
mapLegendPopup.dismiss();
mapLegendButton.getBackground().clearColorFilter();
legendIsShown = false;
} else {
mapLegendPopup.showAtLocation(
findViewById(R.id.buttonMapLegend), Gravity.TOP
| Gravity.LEFT, 8,
(int) (0.23 * getApplicationContext().getResources()
.getDisplayMetrics().heightPixels));
mapLegendButton.getBackground().setColorFilter(
new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000));
// mapLegendButton.getBackground().setColorFilter(0xFFFFFF00,
// PorterDuff.Mode.MULTIPLY);
legendIsShown = true;
}
}
};
I hope this provides an idea of where I'm at. Everything works perfectly well on the Map tab. It's only when you have the Legend shown and switch tabs that it is still displayed on other views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道您说过实现 onPause() 对您不起作用,但我尝试过,并且在 MapActivity 中实现 onResume() 和 onPause() 对我有用。
我需要在 onResume() 中执行 View.post(new Runnable() { ... }) 因为我无法在 onResume() 期间重新创建 popupWindow,所以我必须安排它在 onResume() 之后立即发生:
I know you said that implementing onPause() did not work for you, but I tried it and implementing onResume() and onPause() in the MapActivity does work for me.
I needed to do a View.post(new Runnable() { ... }) in onResume() since I could not recreate the popupWindow during the onResume() so I had to schedule it to occur immediately afterwards:
您可以像这样初始化弹出窗口:
You can init your popupwindown like this:
您应该按照框架推荐的那样,使用 onCreateDialog() 方法来管理对话框< /a>.
这样,您的对话框将成为您的活动的一部分,并且它将自行完成。
如果您确实不想使用它(我看不出任何原因,但仍然如此),您可以在对话框上使用 setOwnerActivity() 将其分配给您的活动。
You should manage your Dialogs by using the onCreateDialog() method, as recommended by the framework.
That way your Dialog will become part of your Activity and it will do it by itself.
If you really don't want to use that (I can't see any reason why that would be the case, but still), you can use the setOwnerActivity() on your Dialog to assign it to your Activity.