4

我有以下功能,它通过单击菜单按钮调用弹出窗口。它有一个确定按钮来关闭弹出窗口。但该onclick功能不会在按下按钮时启动。另外,当按下后退按钮时,我需要关闭弹出窗口。

    LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.about_popup, null, false),400,440, true);

    pw.showAtLocation(lv, Gravity.CENTER, 0, 0);
    View popupView=inflater.inflate(R.layout.about_popup, null, false);
    Button close = (Button) popupView.findViewById(R.id.okbutton);
    close.setOnClickListener(new OnClickListener() {

      public void onClick(View popupView) {
        pw.dismiss();
      }
    });

谢谢

4

3 回答 3

10

当前,您正在将不同的 View 实例传递给PopupWindow并尝试在不同的实例中查找 Button 使用您在 PopupWindow 中传递的相同实例来查找按钮。将您的代码更改为:

  LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View popupView = layoutInflater.inflate(R.layout.about_popup, null, false);
  final PopupWindow pw = new PopupWindow(popupView,400,440, true);

  pw.showAtLocation(lv, Gravity.CENTER, 0, 0);
    Button close = (Button) popupView.findViewById(R.id.okbutton);
    close.setOnClickListener(new OnClickListener() {

      public void onClick(View popupView) {
        pw.dismiss();
      }
    });

第二种方法是使用实PopupWindow​​例在膨胀布局内的当前窗口上再次找到按钮作为按钮:

Button close = (Button) pw.findViewById(R.id.okbutton);
close.setOnClickListener(new OnClickListener() {

          public void onClick(View popupView) {
            pw.dismiss();
          }
        });
于 2013-03-02T05:57:10.853 回答
1
CustomDialogClass cdd = new CustomDialogClass(this,
                "Internet Connection Error",
                "Sorry, no internet connection.Feeds cannot be refreshed",
                "Alert");
        cdd.show();

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {
String txtTitle, txtText, txtType;
Button btn;
TextView alertText;
ImageView imgVw;

public CustomDialogClass(Context context, String title, String text,
        String type) {
    super(context);
    txtTitle = title;
    txtText = text;

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_layout);
    imgVw = (ImageView) findViewById(R.id.iconImgview);

    alertText = (TextView) findViewById(R.id.AlertText);
    alertText.setText(txtText);

    btn = (Button) findViewById(R.id.dismis_dialog);
    btn.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    dismiss();
}
于 2013-03-02T06:01:39.697 回答
0

改变这个

  View popupView=inflater.inflate(R.layout.about_popup, null, false);
    Button close = (Button) popupView.findViewById(R.id.okbutton);

 Button close = (Button) pw.findViewById(R.id.okbutton);
于 2013-03-02T05:57:00.513 回答