我有以下功能,可以通过单击菜单按钮调用弹出窗口。它有一个关闭弹出窗口的确定按钮。但是按下按钮时不会启动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();
}
});
由于
答案 0 :(得分:8)
目前您正在将不同的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();
}
});
答案 1 :(得分: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();
}
答案 2 :(得分: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);