TabActivityで新しいタブに切り替えたときのAndroid PopupWindowを解任?
-
26-09-2019 - |
質問
私はTabActivityにおける4つのタブの一つとしてMapActivityを持っています。このMapActivityは伝説ですPopupWindowを起動することができます。 PopupWindowは「ショー伝説」ボタンをもう一度クリックするまで(前後、など。)。
、マップの上に、画面上に残っています問題は、ユーザーが別のタブに切り替えると、PopupWindowは、ビューの上に永続的なまま、ということです。
私はMapActivityクラスにonPause()メソッドを実装してみました、そしてそこからそれを却下しました。アプリケーションの力は、代わりにこの方法で閉じます。
すべてのヘルプ?ありがとう!
編集:ここに私のコードの一部です:
4つのタブを確立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;
}
}
};
私は、これは私がよどこのアイデアを提供したいと考えています。すべては地図]タブに完璧に適しています。あなたは伝説は、それがまだ他のビューに表示されていることを示し、スイッチタブいるときだけです。
解決
私はあなたがonPause()を実装するあなたのための作業をしなかったことを言ったけど、私はそれを試してみましたが、MapActivityにonResume()とonPause()を実装することは私のための作業を行います。
私は直後に発生し、それをスケジュールする必要がありましたので、私は)onResume(中popupWindowを再作成することができませんでしたので、onResume()にView.post(新しい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);
}
}
);
}
}
他のヒント
あなたはこのようなあなたの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);
あなたは、onCreateDialog()メソッドを使用してダイアログを管理する必要がありますフレームワーク
でます。そのように、あなたのダイアログがあなたの活動の一部になると、それは自分自身でそれを行います。
あなたが本当に(私はそれがまだの場合であってもよいが、なぜ何らかの理由を見ることができない)それを使用したくない場合は、は、あなたの活動に割り当てるためにあなたのダイアログ上)setOwnerActivityを(使用することができます。