【问题标题】:Can't click button on Button in PopupWindow无法单击 PopupWindow 中按钮上的按钮
【发布时间】:2013-04-02 17:30:51
【问题描述】:

当我显示一个包含 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

如何修复?

【问题讨论】:

  • 这是AlertDialog 框吗?或什么样的弹出窗口?为什么不使用包含最多三个可以编码的按钮的警报对话框构建器。

标签: android button click popupwindow


【解决方案1】:

您不会像使用close 按钮那样将他附加到onClickListener。我不知道你想让b 做什么,所以我不能给你任何代码,但你显然知道如何将onClick 事件附加到按钮,因为你已经拥有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();
        }
    });

【讨论】:

  • 不,不。从 XML 加载的按钮关闭效果很好。它有自定义图形。但是以编程方式创建的按钮 b 无法单击 - 当我单击他时,他的图形不会改变。通常按钮 b 用于测试。
  • 我知道。这就是我的回答所涉及的。您没有像为close 那样为它设置onClick(),这就是为什么您可以点击close 而不是b。您需要为b 分配一个onClick(),以便它知道监听click 事件并知道单击它时要做什么。您将更改图形的逻辑或任何您想要的内容放在bonClick()
猜你喜欢
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多