gpt4 book ai didi

android - 无法单击 PopupWindow 中 Button 上的按钮

转载 作者:行者123 更新时间:2023-11-30 03:40:58 25 4
gpt4 key购买 nike

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

自定义对话框.xml:http://pastebin.com/dLivuE17

如何修复?

最佳答案

您没有像使用关闭 按钮那样将他附加到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();
}
});

关于android - 无法单击 PopupWindow 中 Button 上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15770456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com