首先是点击两次返回键退出客户端的方法代码:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Object mHelperUtils;
Toast.makeText(this, "再次点击退出客户端", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
这个方法的不足是当你退出时没有判断是否是在首页,在任何页面双击都将退出客户端,所以应个人要求将判断用这个方法。
第二种是跳出dialog退出客户端
父类parentdialog继承dialog
/**
* 父对话框-所有对话框继承此对话框
*
*/
public class ParentDialog extends Dialog {
private Context context;
private View parentView;
private android.view.View.OnClickListener onComfirmClickListener;
public ParentDialog(Context context, android.view.View.OnClickListener onComfirmClickListener) {
super(context, R.style.parentDialog);
this.setCanceledOnTouchOutside(false);
parentView = LayoutInflater.from(context).inflate(R.layout.parentdialog, null);
this.context = context;
this.onComfirmClickListener = onComfirmClickListener;
setTitle(R.string.parentdialog_title);
}
@Override
public void setContentView(View view) {
FrameLayout parentdialog_content_FrameLayout = (FrameLayout) parentView.findViewById(R.id.parentdialog_content_FrameLayout);
parentdialog_content_FrameLayout.addView(view);
Button cancel = (Button) parentView.findViewById(R.id.parentdialog_cancel_Button);
Button confirm = (Button) parentView.findViewById(R.id.parentdialog_confirm_Button);
cancel.setOnClickListener(onCancelClickListener);
if (onComfirmClickListener == null) {
confirm.setOnClickListener(onCancelClickListener);
} else {
confirm.setOnClickListener(onComfirmClickListener);
}
super.setContentView(parentView);
}
//这个方法随意传入String字符串或者StringBuffer、或StringBuilder
@Override
public void setTitle(CharSequence title) {
TextView textView = (TextView) parentView.findViewById(R.id.parentdialog_title_TextView);
textView.setText(title);
}
<span style="white-space:pre"> </span>//这个方法传入资源文件中的id
@Override
public void setTitle(int titleId) {
TextView textView = (TextView) parentView.findViewById(R.id.parentdialog_title_TextView);
textView.setText(titleId);
}
//调用此方法设置在textView的上下左右显示图标 <span style="font-family: Arial, Helvetica, sans-serif;">setCompoundDrawables实现</span>
public void setIcon(int iconId) {
TextView textView = (TextView) parentView.findViewById(R.id.parentdialog_title_TextView);
textView.setCompoundDrawables(context.getResources().getDrawable(iconId), null, null, null);
}
//设置监听 两个按钮 确定取消
private android.view.View.OnClickListener onCancelClickListener = new android.view.View.OnClickListener() {
@Override
public void onClick(View view) {
ParentDialog.this.cancel();
}
};
}
下面是
parentdialog.xml代码:
最后是exitdialog中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/parent_dialog_bg"
android:minWidth="280dip"
android:orientation="vertical"
>
<TextView
android:id="@id/parentdialog_title_TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="@color/black"
android:textSize="18sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@android:color/darker_gray" />
<FrameLayout
android:id="@id/parentdialog_content_FrameLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="10dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal" >
<Button
android:id="@id/parentdialog_cancel_Button"
android:layout_width="0dp"
android:layout_height="40dip"
android:layout_weight="1"
android:layout_margin="5dip"
android:background="@drawable/dialog_button_bg_selector"
android:gravity="center"
android:padding="3dip"
android:text="@string/cancel"
android:textColor="@android:color/black"
android:textSize="16sp" />
<Button
android:id="@id/parentdialog_confirm_Button"
android:layout_width="0dp"
android:layout_height="40dip"
android:layout_weight="1"
android:layout_margin="5dip"
android:background="@drawable/dialog_button_bg_selector"
android:gravity="center"
android:padding="3dip"
android:text="@string/confirm"
android:textColor="@android:color/black"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
需继承parentdialog:
/**
* 退出对话框
*
*
*/
public class ExitDialog extends ParentDialog {
private Context context;
private static ExitDialog dialog;
public ExitDialog(Context context) {
super(context, onComfirmClickListener);
this.context = context;
setContentView();
dialog = this;
}
<span style="white-space:pre"> </span>//显示dialog提示信息
public void setContentView() {
View setting_netaddress = LayoutInflater.from(context).inflate(
R.layout.exit, null);
super.setContentView(setting_netaddress);
}
private static android.view.View.OnClickListener onComfirmClickListener = new android.view.View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
dialog.cancel();
}
};
}
exit.xml的界面代码:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip"
android:paddingBottom="5.0dp"
android:text="@string/exit"
android:textColor="@color/black"
android:textSize="16.0sp" />
有哪里不明白可以留言一起讨论,谢谢