public class TestLayout extends Activity {
private Button btn;
private PopupWindow popupWindow;
private Button mBtnConfirm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_layout, menu);
return true;
}
public void init(){
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View contentView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.popup, null);
// 设置popupWindow的背景颜色
contentView.setBackgroundColor(Color.GRAY);
// 声明一个弹出框
popupWindow = new PopupWindow(findViewById(R.id.mainlayout), 200, 200);
// 为弹出框设定自定义的布局
popupWindow.setContentView(contentView);
popupWindow.setFocusable(true); // 很重要,获取焦点
popupWindow.showAsDropDown(btn);
mBtnConfirm=(Button)findViewById(R.id.btnConfirm);
mBtnConfirm.setOnClickListener(new OnClickListener(){ //!!!!!!!!!!!!!!!!!!这里报错啊
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
});
}
}
这是我测试popupwindow的代码,layout文件没问题,每次运行,上面标记的地方就出错(求指导啊),onClickListener()
androidpopupWindow问题,代码错误
答案:2 悬赏:10
解决时间 2021-11-14 14:20
- 提问者网友:浪荡羁士
- 2021-11-13 16:06
最佳答案
- 二级知识专家网友:留下所有热言
- 2021-11-13 17:32
需要根据你生成的View下定义控件。使用mBtnConfirm=(Button)contentView.findViewById(R.id.btnConfirm);就可以了。
全部回答
- 1楼网友:duile
- 2021-11-13 18:05
你是想在点击的那个按钮做描点去show popupwindos吧,看样子像popupwindow必须宿主在一个activity里啊,你试试用activity的某个空间做描点,或者让你的按钮传回activity,让activity去show popupwindow
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯