EasyDemo*Android UI篇~PopupWindow(on Github)

点击打开链接

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();

                    }

                });

    

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值