gpt4 book ai didi

android - 在 TabActivity 中切换到新选项卡时关闭 Android PopupWindow?

转载 作者:太空狗 更新时间:2023-10-29 16:25:00 26 4
gpt4 key购买 nike

我有一个 MapActivity 作为 TabActivity 中的四个选项卡之一。这个 MapActivity 可以启动一个 PopupWindow,它是一个图例。 PopupWindow 保留在屏幕上的 map 顶部,直到再次单击“显示图例”按钮(来回等)。

问题是,当用户切换到另一个选项卡时,PopupWindow 在 View 上保持不变。

我试过在 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;
}
}
};

我希望这能让我了解我所处的位置。 map 选项卡上的一切都运行良好。只有当您显示图例并切换选项卡时,它仍会显示在其他 View 中。

最佳答案

我知道你说过实现 onPause() 对你不起作用,但我试过了,在 MapActivity 中实现 onResume() 和 onPause() 对我来说确实有用。

我需要在 onResume() 中执行 View.post(new Runnable() { ... }) 因为我无法在 onResume() 期间重新创建 popupWindow 所以我不得不安排它在之后立即发生:

package com.esri.android.tabdemo;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MapActivity extends Activity
{
private TextView textView = null;
private PopupWindow popupWindow = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Hello World from MapActivity");
setContentView(textView);
}

@Override
protected void onPause()
{
super.onPause();
if (popupWindow != null)
{
popupWindow.dismiss();
popupWindow = null;
}
}

@Override
protected void onResume()
{
super.onResume();
final Context context = this;
textView.post(
new Runnable()
{
public void run()
{
popupWindow = new PopupWindow(context);
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(context);
button.setText("Hello");
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show();
}
});
linearLayout.addView(button);
popupWindow.setContentView(linearLayout);
popupWindow.showAtLocation(linearLayout, Gravity.LEFT | Gravity.BOTTOM, 10, 10);
popupWindow.update(256, 64);
}
}
);
}
}

关于android - 在 TabActivity 中切换到新选项卡时关闭 Android PopupWindow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4226898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com