标签:
创建自定义对话框:
// 对话框构建器 Builder builder = new AlertDialog.Builder(this); // 创建出一个空的对话框 final AlertDialog dialog = builder.create(); // 加载自定义View布局 View view = View.inflate(this, R.layout.custom_dialog, null); int viewSpacingLeft = 0; int viewSpacingTop = 0; int viewSpacingRight = 0; int viewSpacingBottom = 0; // 给对话框指定自定义的layout文件,并且上下左右边框为0:不然对话框会出现黑框,因为低版本的对话框,不指定背景的话就是黑色的。 dialog.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom); // 找到button,设置点击事件 Button btn_login = (Button) view.findViewById(R.id.btn_login); Button btn_cancel = (Button) view.findViewById(R.id.btn_cancel); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "确定按钮被点击了", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "取消按钮被点击了", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); // 显示对话框 dialog.show();
标签:
原文地址:http://www.cnblogs.com/caidupingblogs/p/5218704.html