PopupWindow对话框
使用PopupWindow有以下两步:
1. 调用PopupWindow的构造器创建PopupWindow对象。
2. 调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来,调用PopupWindow的showAt
Location(View v,int Gravity,intx,iny y)将PopupWindow显示在指定位置。
PopupWindow的使用例子代码如下:
public class PopuWindowTest extends Activity {
private LinearLayout ll_user_insert;
private Button btn_cancel;
private Button btn_sure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 加载R.layout.popup对应的界面布局
View root = this.getLayoutInflater().inflate(R.layout.popup, null);
// 创建出PopupWindow对象
final PopupWindow popup = new PopupWindow(root, 500, 400);
// 通过findViewById()找出ll_user_insert的id
LinearLayout ll_user_insert = (LinearLayout) findViewById(R.id.ll_user_insert);
btn_sure = (Button)findViewById(R.id.btn_sure);
// 为ll_user_insert添加监听器
ll_user_insert.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// popup.showAsDropDown(v);
// 将PopupWindow显示在指定位置
popup.showAtLocation(findViewById(R.id.ll_user_insert),
Gravity.LEFT, 200, 0);
}
});
// 为PopupWindow中的取消按钮实现监听功能
root.findViewById(R.id.btn_cancel).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popup.dismiss();// 点击取消按钮关闭弹窗
}
});
// 为PopupWindow中的确定按钮实现监听功能
root.findViewById(R.id.btn_cancel).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
AlertDialogDemo.this);
builder.setTitle("提示");
builder.setMessage("请先选择角色名称");
builder.create().show();
}
});
}
}