android中使用PopupWindow实现取消确定对话框
2016-09-28 15:11
549 查看
需求背景:app退出登录时,弹出取消确定对话框。
个人感觉使用AlertDialog,需要修改系统自带的样式,比较麻烦,所以这里就用PopupWindow来实现这一需求。
这里先展示一下最终界面:
![](http://img.blog.csdn.net/20160928150234997?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
实现代码如下:
一、对话框xml代码(dialog_login_out.xml):
二、java代码(UserCenterActivity.java):
ok,结束。
个人感觉使用AlertDialog,需要修改系统自带的样式,比较麻烦,所以这里就用PopupWindow来实现这一需求。
这里先展示一下最终界面:
实现代码如下:
一、对话框xml代码(dialog_login_out.xml):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_dialog_login_out" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#66000000"> <LinearLayout android:id="@+id/ll_popup_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_white1" android:orientation="vertical" android:layout_centerInParent="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp" android:paddingBottom="10dp" android:orientation="vertical" android:background="@drawable/border_bottom_gray"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提示" android:layout_gravity="center_horizontal" android:padding="2dp" android:textColor="#000000" android:textSize="12sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否确定退出登录" android:textColor="#000000" android:layout_gravity="center_horizontal" android:textSize="10sp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bt_cancel" android:layout_width="100dp" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:text="取消" android:textSize="13sp" android:textColor="#027bff" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:background="@drawable/border_right"/> <Button android:id="@+id/bt_ok" android:layout_width="100dp" android:layout_height="wrap_content" android:paddingTop="8dp" android:paddingBottom="8dp" android:text="确定" android:textSize="13sp" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" android:textColor="#027bff" android:background="@null"/> </LinearLayout> </LinearLayout> </RelativeLayout>
二、java代码(UserCenterActivity.java):
.... private PopupWindow mPopWindow; .... //退出 private void loginOut(){ //获取自定义view View mDialogView = LayoutInflater.from(this).inflate(R.layout.dialog_login_out, null); mPopWindow = new PopupWindow(mDialogView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true); mPopWindow.setOutsideTouchable(false); //显示PopupWindow View rootview = LayoutInflater.from(this).inflate(R.layout.activity_usercenter, null); mPopWindow.showAtLocation(rootview, Gravity.CENTER, 0, 0); //拦截点击事件,防止传到下一层 RelativeLayout rl_dialog_login_out=(RelativeLayout)mDialogView.findViewById(R.id.rl_dialog_login_out); rl_dialog_login_out.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); //取消按钮 Button bt_cancel=(Button)mDialogView.findViewById(R.id.bt_cancel); bt_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPopWindow.dismiss(); } }); //确定退出 Button bt_ok=(Button)mDialogView.findViewById(R.id.bt_ok); bt_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //.....do something } }); }
ok,结束。
相关文章推荐
- 新手求教android的checkBox问题,现在我想实现按一个按钮,弹出一个对话框,这个对话框有标题,两列多行的checkBox以及最后的确定取消按钮,咋做?
- Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框
- Android PopupWindow弹出对话框的实现
- Android:创建常见对话框以及使用对话框实现登陆
- android弹出对话框中带有确定和取消对话框带事件的应用
- android使用NumberPicker实现自定义带时间的对话框
- Android 使用DialogFragment 对话框实现
- Android: 创建一个AlertDialog对话框,必须按确定或取消按钮才能关闭对话框,禁止按[返回键]或[搜索键]关闭
- Android -- 对话框 Dialog: 确定取消,单选多选,进度显示, 常用控件示例:单选多选框,下拉列表,进度条,自动完成文本框, 菜单显示
- Android开发技巧——使用PopupWindow实现弹出菜单
- Android 点击AlertDialog上的确定和取消按钮,使对话框不消失
- Android 对话框AlertDialog,PopupWindow使用大全
- android中使用PopupWindow实现弹出窗口菜单
- Android下关于AlertDialog点击 确定或者取消按钮 对话框自动消失的 解决办法
- Android开发技巧——使用PopupWindow实现弹出菜单
- android Dialog的确定按钮点击后不取消对话框
- android-使用PopupWindow实现随机排列的自定义密码键盘
- 【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单
- TextView使用SpannableString设置复合文本 + android弹出窗口的实现(PopupWindow)
- Android的自定义Menu使用PopupWindow实现