1,创建dialog的布局,如
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical"> <TextView android:layout_height="wrap_content" android:layout_width="match_parent" android:text="下线通知" android:layout_margin="@dimen/widget_margin_15" android:textColor="@color/blue_0" android:textSize="@dimen/font_size_18" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/blue_0"/> <TextView android:id="@+id/token_err" android:textColor="@color/black" android:textSize="@dimen/font_size_16" android:layout_margin="@dimen/widget_margin_10" android:layout_height="wrap_content" android:layout_width="match_parent" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/list_split_line_color"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/pop_exit" android:text="退出" android:paddingBottom="@dimen/widget_margin_15" android:paddingTop="@dimen/widget_margin_15" android:layout_weight="1" android:textColor="#0000ff" android:background="@color/white" android:textSize="@dimen/font_size_18" android:layout_width="match_parent" android:layout_height="wrap_content" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/list_split_line_color"/> <Button android:id="@+id/pop_relogin" android:text="重新登陆" android:paddingBottom="@dimen/widget_margin_15" android:paddingTop="@dimen/widget_margin_15" android:textSize="@dimen/font_size_18" android:textColor="#0000ff" android:background="@color/white" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>2,在style中声明如下风格
<style name="tokenDialog" parent="@android:style/Theme.Dialog"> <item name="android:layout_marginLeft">10dp</item> <item name="android:layout_marginRight">10dp</item> <item name="android:windowNoTitle">true</item><!--无标题,布局中自定义标题--> <item name="android:windowBackground">@color/transparent</item><!--背景透明,用于宽或高有边距--> <!--的时候不出现黑边--> </style>3,在需要创建的地方调用下列方法
/** * 登陆信息过期显示弹窗 * @param ctx context对象 */ private static void initErrDialog(final Context ctx) { final Dialog dialog=new Dialog(ctx,R.style.tokenDialog); Window dialogWindow = dialog.getWindow(); dialogWindow.setGravity(Gravity.CENTER);//显示在中心 dialog.setCanceledOnTouchOutside(false);//设置点击Dialog外部任意区域关闭Dialog View view= LayoutInflater.from(ctx).inflate(R.layout.pop_token_outofdate_tips, null); Button exitbtn= (Button) view.findViewById(R.id.pop_exit); Button reloginBtn= (Button) view.findViewById(R.id.pop_relogin); TextView errTxt= (TextView) view.findViewById(R.id.token_err); Long timemills=System.currentTimeMillis(); Date dat = new Date(timemills); GregorianCalendar gc = new GregorianCalendar();//GregorianCalendar 是Calendar 的一个具体子类 gc.setTime(dat); SimpleDateFormat format = new SimpleDateFormat("HH:mm");//24小時制 String time = format.format(gc.getTime()); errTxt.setText(Html.fromHtml("您的账号于" + time + "在另一部手机登录。如非本人操作,则密码可能已泄露," + "建议前往<font color=blue>http://www.daokoudai.com</font>修改密码。")); exitbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); android.os.Process.killProcess(android.os.Process.myPid());//退出整个应用 } }); reloginBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); TabMainActivity.getInst().finish();//finish()当前的activity,不然回来还会有dialog //不知道dialog.dismiss()为何异常 TabMainActivity.getInst().startActivity(new Intent(TabMainActivity.getInst(), LoginActivity.class)); } }); dialog.setContentView(view); dialog.show(); }效果图