1

当我显示包含 Button 的 PopupWindow 时,我无法单击他。执行 onclick,但不更改图形。

它显示弹出窗口:

public void btn_friendsgame_newClick(View v) {
    LayoutInflater layoutInflater = 
            (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  

    View popupView = layoutInflater.inflate(R.layout.custom_dialog, null);

    RelativeLayout content = (RelativeLayout) popupView.findViewById(R.id.layout_content);



    Button b = new Button(this); // THIS BUTTON CAN'T CLICK!



    b.setText("Test");
    b.setLayoutParams(new LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    content.addView(b);

    final PopupWindow popupWindow = new PopupWindow(popupView, 
               LayoutParams.MATCH_PARENT,  LayoutParams.WRAP_CONTENT);  

    popupWindow.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
    Button close = (Button) popupView.findViewById(R.id.btn_popup_settings);
    close.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });

custom_dialog.xml:http ://pastebin.com/dLivuE17

如何修复这个?

4

1 回答 1

1

您不会onClickListener像使用按钮那样将他附加到一个close按钮上。我不知道你想做b什么,所以我不能给你任何代码,但你显然知道如何将onClick事件附加到按钮,因为你已经有了 with close。做同样的事情,b把你需要的任何逻辑放在里面onClick(),你应该可以点击他。

public void btn_friendsgame_newClick(View v) {
    LayoutInflater layoutInflater = 
            (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  

    View popupView = layoutInflater.inflate(R.layout.custom_dialog, null);

    RelativeLayout content = (RelativeLayout) popupView.findViewById(R.id.layout_content);



    Button b = new Button(this); // THIS BUTTON CAN'T CLICK!



    b.setText("Test");
    b.setLayoutParams(new LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    content.addView(b);

   // here set onClick
   b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        // put logic in here to do whatever you want
      }


    final PopupWindow popupWindow = new PopupWindow(popupView, 
               LayoutParams.MATCH_PARENT,  LayoutParams.WRAP_CONTENT);  

    popupWindow.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);
    Button close = (Button) popupView.findViewById(R.id.btn_popup_settings);
    close.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    });
于 2013-04-02T17:33:51.867 回答