简体   繁体   English

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

[英]How to popupWindow from different activities

I started Android programming like 3 days ago and I'm getting trouble with the PopupWindows. 我三天前开始进行Android编程,但是PopupWindows遇到了麻烦。 My idea is to show some detail about my app in the context menu (the default button is called Settings) so when the button is cliced, a Popupwindow must appear. 我的想法是在上下文菜单(默认按钮称为“设置”)中显示有关我的应用程序的一些详细信息,因此当该按钮出现问题时,必须出现一个Popupwindow。

I've here my class, the one I should call from every other class relatid with any activity in order to show the popup. 我在这里是我的班级,我应该从与任何活动相关的其他所有班级中调用,以显示弹出窗口。

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

} The dismiss is related with a button that the popupwindow has in order to close it. }关闭与popupwindow用来关闭它的按钮有关。 Now my question is, how can I call that popup from every class so I can avoid copying the code in every single of them? 现在我的问题是,如何从每个类中调用该弹出窗口,以免避免在每个类中复制代码?

I should make that one static but if i do that, method like this.getCurrentFocus(); 我应该使该静态,但如果我这样做,像this.getCurrentFocus();这样的方法; get unusable. 变得无法使用。

I'm not 100% of whatevery syntax word does because I invested like 3-4 hours looking for how to popup the window from that menu so, for example, I could tell you that MenuItem item must be called there because somehow it recognizes when the user clicks the button. 我不是100%的语法单词,因为我花了3-4个小时的时间来寻找如何从该菜单中弹出窗口,因此,例如,我可以告诉您必须在那里调用MenuItem项,因为它可以识别何时用户单击按钮。

Thank you for your help:) 谢谢您的帮助:)

Just Create a object of this class using context parameter and then call shoWwindow(): 只需使用上下文参数创建此类的对象,然后调用shoWwindow():

In Activity call: 在活动调用中:

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