Android中AlertDialog的简单使用
来源:互联网 发布:电信4g是什么网络制式 编辑:程序博客网 时间:2024/05/24 02:47
在AlertDialog使用详解中,非常详细的介绍了以下六种使用方法。
一、简单的AlertDialog(只显示一段简单的信息,比如about us)
二、带按钮的AlertDialog(显示提示信息,让用户操作,比如exit时的警告框)
三、类似ListView的AlertDialog(展示内容,比如某人的一些注册信息)
四、类似RadioButton的AlertDialog(让用户选择,单选)
五、类似CheckBox的AlertDialog(让用户多选)
六、自定义View的AlertDialog(当以上方式满足不了你的需求,就要自定义了)
在现实中,我们常常会遇到按下退出键然后弹出一个窗口,询问我们是否确认退出,要做到这样的效果的方法有许多种,而今天我给大家来介绍的是一种较为简单的实现方法,下面上代码。
MainActivity.java
package com.whisker.backcheck;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.view.KeyEvent;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) && event.getRepeatCount() == 0){dialogExit(MainActivity.this);}return true;}public static void dialogExit(Context context){AlertDialog.Builder builder = new Builder(context);builder.setTitle("提示");builder.setMessage("确定要退出吗?");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {arg0.dismiss();android.os.Process.killProcess(android.os.Process.myPid());}});builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {arg0.dismiss();}});builder.create().show();}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.whisker.backcheck.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
下面是效果图:
0 0
- Android中AlertDialog的简单使用
- android AlertDialog的简单使用
- Android中AlertDialog的使用
- Android中AlertDialog的使用
- Android 中 AlertDialog的使用
- Android中AlertDialog的使用
- Android简单弹出对话框AlertDialog的使用
- AlertDialog的简单使用
- Android中AlertDialog使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog和Toast的使用
- Android中AlertDialog对话框的使用
- Android中AlertDialog和ProgressDialog的使用
- android studio中AlertDialog的使用
- Android中AlertDialog(对话框)的使用
- Android中AlertDialog等的使用
- union
- eclipse修改编辑字体大小
- hdoj 1005 NUMBER Sequence
- wpf 如何将命名空间导入 XAML
- 小樽中文离线地图App上线
- Android中AlertDialog的简单使用
- C语言,猜字游戏
- 第八章:加载Maya2011模型
- hdoj Let the Balloon Rise
- android PorterDuffXfermode ,PorterDuff.Mode 使用 以及Porter-Duff规则详解
- LoadRunner Controller阶段,脚本运行正常Passed Transactions 和Failed Transactions Errors 都没有数据显示?
- ArrayList遍历的4种方法
- QT学习资源
- 札幌中文离线地图App上线