2

TabActivityの4つのタブの1つとしてMapActivityがあります。このMapActivityは、凡例であるPopupWindowを起動できます。PopupWindowは、[凡例を表示]ボタンが再度クリックされるまで(前後など)、マップの上部の画面に表示されたままになります。

問題は、ユーザーが別のタブに切り替えたときに、PopupWindowがビュー上で永続的なままになることです。

MapActivityクラスにonPause()メソッドを実装し、そこから削除してみました。この方法を実行すると、適用力が閉じます。

何か助けはありますか?ありがとう!

編集:これが私のコードの一部です:

MainActivityで、4つのタブを確立します。

    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 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.

4

3 に答える 3

3

onPause()の実装はうまくいかなかったとおっしゃっていましたが、試してみたところ、MapActivityにonResume()とonPause()を実装してもうまくいきました。

onResume()中にpopupWindowを再作成できなかったため、onResume()でView.post(new Runnable(){...})を実行する必要がありました。そのため、直後に発生するようにスケジュールする必要がありました。

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);
                }
            }
        );
    }
}
于 2012-07-02T23:35:13.643 に答える
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);
于 2012-02-28T07:32:01.737 に答える
1

フレームワークで推奨されているように、onCreateDialog()メソッドを使用してダイアログを管理する必要があります。

そうすれば、ダイアログはアクティビティの一部になり、それ自体で実行されます。

本当にそれを使用したくない場合(それが当てはまる理由はわかりませんが、それでも)、ダイアログでsetOwnerActivity()を使用してアクティビティに割り当てることができます。

于 2010-11-19T16:24:04.907 に答える