【问题标题】:Closing the Popup in Android [duplicate]在Android中关闭弹出窗口[重复]
【发布时间】:2013-03-02 05:47:33
【问题描述】:

我有以下函数,它通过单击菜单按钮调用弹出窗口。它有一个确定按钮来关闭弹出窗口。但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();
      }
    });

谢谢

【问题讨论】:

    标签: android android-widget popup


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,我知道错了。改为 View popupView = inflater.inflate(R.layout.about_popup, null, false);
    【解决方案2】:
    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();
    }
    

    【讨论】:

      【解决方案3】:

      改变这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-03
        • 1970-01-01
        • 2014-06-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多