Может android PopupWindow показать другой PopupWindow?
Может ли андроид PopupWindow показать другой PopupWindow?
Сколько PopupWindow можно открыть за одно и то же время? Только один?
Первый PopupWindow отображается нормально. Но при нажатии кнопки (которая находится в первом всплывающем окне PopupWindow) у меня есть исключение:
08-13 16:28:38.682: ERROR/AndroidRuntime(11760): FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:600)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:313)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:992)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:901)
at org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:159)
at org.example.qberticus.quickactions.BetterPopupWindow.showLikePopDownMenu(BetterPopupWindow.java:129)
at name.antonsmirnov.android.popup.ui.MainActivity$1$1.run(MainActivity.java:44)
at android.app.Activity.runOnUiThread(Activity.java:4170)
at name.antonsmirnov.android.popup.ui.MainActivity$1.onClick(MainActivity.java:42)
at android.view.View.performClick(View.java:3558)
at android.view.View$PerformClick.run(View.java:14157)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4514)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
код:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bindControls();
initControls();
}
private Button button;
private void bindControls() {
button = (Button) findViewById(R.id.button);
}
private void initControls() {
initButton(button);
}
private void initButton(final Button button) {
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
final BetterPopupWindow window = new BetterPopupWindow(button);
View popupview = createPopupView();
window.setContentView(popupview);
runOnUiThread(new Runnable() {
public void run() {
window.showLikePopDownMenu();
}
});
}
});
}
private View createPopupView() {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.window, null);
Button popupButton = (Button) v.findViewById(R.id.popupbutton);
initButton(popupButton);
return v;
}
Ответы
Ответ 1
После игры я обнаружил, что
window.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, x, y);
работает нормально, но
window.showAsDropDown(getWindow().getDecorView(), Gravity.CENTER, x, y);
вызывает исключение! Если вы используете showAtLocation(view)
с любым видом, отличным от getWindow().getDecorView()
, у вас все еще будет исключение.
Ответ 2
Да, это возможно, но это не рекомендуется. Есть способ сделать это, но он не работает на Android 2.1 или меньше. Вот пример кода:
public class AlertDialogWithDialog extends AlertDialog implements OnClickListener {
private boolean dirtyHackOnBackPressed = true;
protected AlertDialogWithDialog(Context context) {
super(context);
setButton(BUTTON_POSITIVE, "OK", this);
setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && dirtyHackOnBackPressed) {
if(dirtyHackOnBackPressed){
DialogUtils dialogUtils = new DialogUtils(getContext(), AlertDialogWithDialog.this);
DialogUtils.createDialog();
dirtyHackOnBackPressed = false;
}
return true;
} else {
dirtyHackOnBackPressed = true;
return false;
}
}
});
}
@Override
public void show() {
super.show();
final Button cancelButton =
getButton(DialogInterface.BUTTON_NEGATIVE);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogUtils dialogUtils = new DialogUtils(getContext(), AlertDialogWithDialog.this);
dialogUtils.createDialog();
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}
И класс Utils:
package com.example.utils;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;
/**
*
* @author Piotr Slesarew
*
*/
public class DialogUtils {
private String title = "...";
private String positiveButtonText = "Yes";
private String negativeButtonText = "No";
private String messageText = "....?";
private String toastText = "....";
private Context context;
private AlertDialog alertDialog;
Dialog dialog;
public DialogUtils(Context context, Dialog dialog) {
super();
this.context = context;
this.dialog = dialog;
}
/**
*
* @param positiveButtonText
* @param negativeButtonText
* @param messageText
* @param toastText
* @param context
*/
public DialogUtils(String positiveButtonText, String negativeButtonText,
String messageText, String toastText, Context context) {
super();
this.positiveButtonText = positiveButtonText;
this.negativeButtonText = negativeButtonText;
this.messageText = messageText;
this.toastText = toastText;
this.context = context;
}
public void createDialog(){
alertDialog = new AlertDialog.Builder(this.context).create();
alertDialog.setTitle(title);
alertDialog.setButton(positiveButtonText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});
alertDialog.setButton(positiveButtonText, createPositiveOnClickListener());
alertDialog.setButton2(negativeButtonText, createNegativeOnClickListener());
alertDialog.setMessage(messageText);
alertDialog.show();
}
private DialogInterface.OnClickListener createPositiveOnClickListener(){
return new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
IziDialogUtils.this.dialog.dismiss();
Toast.makeText(context, "Porzucono zmiany", Toast.LENGTH_SHORT).show();
}
};
}
private DialogInterface.OnClickListener createNegativeOnClickListener() {
return new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
};
}
}
Ответ 3
Да, вы можете. но здесь, посмотрев на трассировку стека исключений, кажется, что вы даете неверный контекст. попробуйте запустить другое диалоговое окно с тем же контекстом или контекстом вашей деятельности, если это не дочерний вид любого родителя, такого как TabView.