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

时间:2010-11-19 16:10:40

标签: android popupwindow dismiss tabactivity

我将MapActivity作为TabActivity中的四个选项卡之一。此MapActivity可以启动一个传奇的PopupWindow。 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;
        }
    }
};

我希望这可以让我知道我在哪里。一切都在Map选项卡上完美运行。只有当您显示图例并切换选项卡时,它才会显示在其他视图中。

3 个答案:

答案 0 :(得分:3)

我知道你说实现onPause()并不适合你,但是我尝试了它并在MapActivity中实现onResume()和onPause()对我有用。

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

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

答案 1 :(得分:2)

你可以像这样初始化你的popupwindown:

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

答案 2 :(得分:1)

您应按照建议的by the framework使用onCreateDialog()方法管理对话框。

这样你的Dialog就会成为你活动的一部分,它会自己完成。

如果你真的不想使用它(我看不出任何原因,但仍然如此),你可以使用Dialog上的setOwnerActivity()将它分配给你的Activity。 / p>