需求:

点页面,出现弹层,弹层包含EditText,Button等,点击Button实现提交操作;

最终代码:

private PopupWindow popupWindow ;
private EditText gfName;
private View popView;
private Button addGf; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
   //
   popView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.popup_add_gf, null); gfName = (EditText)popView.findViewById(R.id.gfname);
addGf = (Button)popView.findViewById(R.id.addgf); // 创建PopupWindow对象
popupWindow = new PopupWindow(popView, 600, 800); //指定高度宽度
// popupWindow = new PopupWindow(popView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);//充满屏幕 // 为弹出框设定自定义的布局
popupWindow.setContentView(popView);
// 使其聚焦
popupWindow.setFocusable(true);
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new BitmapDrawable());
addGf.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG,"Button onClick");
} gfName.setText(addressName);

遇到的问题及解决:

1、操作编辑框setText 及按钮setOnClickListener 时报错,空指针。

解决:初始化元素时要指定view,如:

gfName = (EditText)popView.findViewById(R.id.gfname);

不要这样:

mapView = (MapView) findViewById(R.id.map);

接下来就是对弹层的美化了,参考:

兑现半透明的popupwindow的源码

http://www.myexception.cn/mobile/620613.html

展示PopupWindow

http://www.myexception.cn/mobile/430920.html

Android popupwindow 实现登录账户选择

http://hi.baidu.com/guoxiaoming/item/5b0b165d017efa3e33e0a9f2

android(九种对话框)的实现方式

http://www.2cto.com/kf/201204/128926.html

05-08 15:32