繁体   English   中英

如何从不同的活动弹出窗口

[英]How to popupWindow from different activities

我三天前开始进行Android编程,但是PopupWindows遇到了麻烦。 我的想法是在上下文菜单(默认按钮称为“设置”)中显示有关我的应用程序的一些详细信息,因此当该按钮出现问题时,必须出现一个Popupwindow。

我在这里是我的班级,我应该从与任何活动相关的其他所有班级中调用,以显示弹出窗口。

public class MostrarDetallesApp extends Activity implements View.OnClickListener {

PopupWindow popupWindow;

public void detallesApp(MenuItem item) {
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.detallesaplicacion, null);
    View background = this.getCurrentFocus();
    popupWindow = new PopupWindow(
            popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);


    Button cerrarDetallesApp = (Button)popupView.findViewById(R.id.cerrarDetallesAppButton);
    cerrarDetallesApp.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    popupWindow.showAtLocation(background,Gravity.CENTER,Gravity.CENTER,Gravity.CENTER);
    popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
    popupWindow.setFocusable(true);
    popupWindow.update();


}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    popupWindow.dismiss();
}

}关闭与popupwindow用来关闭它的按钮有关。 现在我的问题是,如何从每个类中调用该弹出窗口,以免避免在每个类中复制代码?

我应该使该静态,但如果我这样做,像this.getCurrentFocus();这样的方法; 变得无法使用。

我不是100%的语法单词,因为我花了3-4个小时的时间来寻找如何从该菜单中弹出窗口,因此,例如,我可以告诉您必须在那里调用MenuItem项,因为它可以识别何时用户单击按钮。

谢谢您的帮助:)

只需使用上下文参数创建此类的对象,然后调用shoWwindow():

在活动调用中:

MostrarDetallesApp obj=new MostrarDetallesApp(this);
obj.showWindow();


public class MostrarDetallesApp implements View.OnClickListener {
PopupWindow popupWindow;
private Context context;

MostrarDetallesApp(Context context) {
    this.context = context;
}

public void showWindow() {
    LayoutInflater layoutInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.detallesaplicacion,
            null);
    View background = this.getCurrentFocus();
    popupWindow = new PopupWindow(popupView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);

    Button cerrarDetallesApp = (Button) popupView
            .findViewById(R.id.cerrarDetallesAppButton);
    cerrarDetallesApp.setOnClickListener(new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });
    popupWindow.showAtLocation(background, Gravity.CENTER, Gravity.CENTER,
            Gravity.CENTER);
    popupWindow.setAnimationStyle(android.R.style.Animation_Toast);
    popupWindow.setFocusable(true);
    popupWindow.update();

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    popupWindow.dismiss();
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM