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

发布于 2024-10-03 17:58:02 字数 3726 浏览 0 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

挽袖吟 2024-10-10 17:58:02

我知道您说过实现 onPause() 对您不起作用,但我尝试过,并且在 MapActivity 中实现 onResume() 和 onPause() 对我有用。

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

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

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:

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);
                }
            }
        );
    }
}
枫林﹌晚霞¤ 2024-10-10 17:58:02

您可以像这样初始化弹出窗口:

mapLegendPopup = new PopupWindow(this);
mapLegendPopup.setContentView (itemizeView);
mapLegendPopup.setBackgroundDrawable (new BitmapDrawable()); // key is here
mapLegendPopup.setWidth ((int) (0.45 * getApplicationContext().getResources()
                    .getDisplayMetrics().widthPixels));
mapLegendPopup.setHeight((int) (0.33 * getApplicationContext().getResources()
                    .getDisplayMetrics().heightPixels));
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);

You can init your popupwindown like this:

mapLegendPopup = new PopupWindow(this);
mapLegendPopup.setContentView (itemizeView);
mapLegendPopup.setBackgroundDrawable (new BitmapDrawable()); // key is here
mapLegendPopup.setWidth ((int) (0.45 * getApplicationContext().getResources()
                    .getDisplayMetrics().widthPixels));
mapLegendPopup.setHeight((int) (0.33 * getApplicationContext().getResources()
                    .getDisplayMetrics().heightPixels));
mapLegendPopup.setFocusable(false);
mapLegendPopup.setOutsideTouchable(true);
两个我 2024-10-10 17:58:02

您应该按照框架推荐的那样,使用 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.

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