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

时间:2014-11-14 13:22:15

标签: java android popup

我在3天前启动了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();
}

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

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

我不是百分之百的语法词,因为我花了3-4个小时来寻找如何从该菜单弹出窗口,所以,例如,我可以告诉你,必须在那里调用MenuItem项,因为不知何故它会在用户单击按钮时识别。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

使用context参数创建此类的对象,然后调用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();
}

}