Android Dialog大全

1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

  代码:

  创建对话框方法dialog()
  protected void dialog() {
    AlertDialog.Builder builder = new Builder(Main.this);
    builder.setMessage("确认退出吗?");
    builder.setTitle("提示");
    builder.setPositiveButton("确认", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
      Main.this.finish();
     }
    });
    builder.setNegativeButton("取消", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      dialog.dismiss();
     }
    });
    builder.create().show();
   }
 
  在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
     dialog();
    }
    return false;
   }

  2.改变了对话框的图表,添加了三个按钮

  Dialog dialog = new AlertDialog.Builder(this).setIcon(
       android.R.drawable.btn_star).setTitle("喜好调查").setMessage(
       "你喜欢李连杰的电影吗?").setPositiveButton("很喜欢",
       new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
         // TODO Auto-generated method stub
         Toast.makeText(Main.this, "我很喜欢他的电影。",
           Toast.LENGTH_LONG).show();
        }
       }).setNegativeButton("不喜欢", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(Main.this, "我不喜欢他的电影。", Toast.LENGTH_LONG)
         .show();
      }
     }).setNeutralButton("一般", new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       Toast.makeText(Main.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG)
         .show();
      }
     }).create();
     dialog.show();

  3.信息内容是一个简单的View类型

  new AlertDialog.Builder(this).setTitle("请输入").setIcon(
       android.R.drawable.ic_dialog_info).setView(
       new EditText(this)).setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();

  4.信息内容是一组单选框

  new AlertDialog.Builder(this).setTitle("复选框").setMultiChoiceItems(
       new String[] { "Item1", "Item2" }, null, null)
       .setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();

 5.信息内容是一组多选框

  new AlertDialog.Builder(this).setTitle("单选框").setIcon(
       android.R.drawable.ic_dialog_info).setSingleChoiceItems(
       new String[] { "Item1", "Item2" }, 0,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
        }
       }).setNegativeButton("取消", null).show();

  6.信息内容是一组简单列表项

  new AlertDialog.Builder(this).setTitle("列表框").setItems(
       new String[] { "Item1", "Item2" }, null).setNegativeButton(
       "确定", null).show();

  7.信息内容是一个自定义的布局

  布局文件
  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content" android:layout_width="wrap_content"
   android:background="#ffffffff" android:orientation="horizontal"
   android:id="@+id/dialog">
   <TextView android:layout_height="wrap_content"
     android:layout_width="wrap_content"
    android:id="@+id/tvname" android:text="姓名:" />
   <EditText android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>
  </LinearLayout>
  调用代码
  LayoutInflater inflater = getLayoutInflater();
     View layout = inflater.inflate(R.layout.dialog,
       (ViewGroup) findViewById(R.id.dialog));
     new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
       .setPositiveButton("确定", null)
       .setNegativeButton("取消", null).show();

时间: 2024-08-30 06:49:16

Android Dialog大全的相关文章

android项目大全,总有你所需的

注:打开请贴网址.有些直接通过链接打开的不对. 1.相对布局实例 http://kukuqiu.iteye.com/blog/1018396 2.Log图文具体解释(Log.v,Log.d,Log.i,Log.w,Log.e)(转) http://www.cnblogs.com/menglin2010/archive/2011/12/20/2294338.html3. getResources()方法的作用和要点http://blog.sina.com.cn/s/blog_9f4bc8e3010

android权限大全

android权限大全 访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES ,读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permission.ACCESS_COARSE_LOCATION,通过WiFi或移动基站的方式获取用户错略的经纬度信息,定位精度大概误差在30~1500米 获取精确位置 android.permission.ACCESS_FINE_LOCATION,通过GPS芯片接收卫星的定位信息,定位

Android dialog 全屏

Android中让Dialog全屏: 一.在style中定义样式: <?xml version="1.0" encoding="utf-8"?> <resources> <style name="Transparent"> <item name="android:windowBackground">@color/transparent_background</item>

7种形式的Android Dialog使用举例

7种形式的Android Dialog使用举例 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助. 1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式. 创建dialog对话框方

Android Dialog 的一些特性

1. Dialog 与 AlertDialog 的区别. AlertDialog 是一种特殊形式的 Dialog.这个类中,我们可以添加一个,两个或者三个按钮,可以设置标题.所以,当我们想使用 AlertDialog 默认的按钮形式,用 AlertDialog 更加方便,而且有一个类 AlertDialog.Builder 很方便创建一个 AlertDialog. 2. Dialog 与 AlertDialog 写代码时需注意的事项. 我们可以给一个 Dialog 用自定义的 Layout.有两

几种不同的Android Dialog

在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助. 1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式. 创建dialog对话框方法代码如下: ? 1 2 3 4 5 6 7 8

Android Permission denied 错误 ( 附Android权限大全 )

Android Permission denied 错误(附Android权限大全) java.net.SocketException: Permission denied (maybe missing INTERNET permission) 这是一个经典错误, Socket不能对外连接,错误不会被报出,调试的时候,能看到Exception, 这个Exception会有非常多变体. Android默认不同意訪问网络,所以,在AndroidManifest.xml中,须要进行例如以下配置: <u

在 ANDROID DIALOG中使用

我想再android Dialog中使用 autocompletetext, 在开发测试过程中碰见不少问题,把最后的成品发布出来,避免大家走弯路,下面的代码是完整代码详细代码如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5

Android颜色大全

Android颜色大全 颜  色    RGB值 英文名 中文名 #FFB6C1 LightPink 浅粉红 #FFC0CB Pink 粉红 #DC143C Crimson 深红/猩红 #FFF0F5 LavenderBlush 淡紫红 #DB7093 PaleVioletRed 弱紫罗兰红 #FF69B4 HotPink 热情的粉红 #FF1493 DeepPink 深粉红 #C71585 MediumVioletRed 中紫罗兰红 #DA70D6 Orchid 暗紫色/兰花紫 #D8BFD8