Android控件开发之Dialog

 我们今天就来看看dialog类。其实我们的dialog在应用里用到的地方很多,也是最常用的一个类。那我们怎么样来实现一个简单的漂浮窗口那,完成在activity中创建。使用基本的dialog类,你可以创建一个新的实例并设定标题和布局。

效果图

 

 

 本程序main.xml源码

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<Button 
    android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="默认样式" />  
<Button
    android:id="@+id/btn2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="改变图标样式" />
<Button 
    android:id="@+id/btn3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="获取输入内容样式" /> 
<Button 
    android:id="@+id/btn4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="单选项列表样式" /> 
<Button 
    android:id="@+id/btn5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="多选项列表样式" /> 
<Button 
    android:id="@+id/btn6"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="信息内容为列表样式" /> 
<Button 
    android:id="@+id/btn7"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="登录样式" />  
<Button 
    android:id="@+id/btn8"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="自定义样式" />
</LinearLayout>


 

 AlertDialog类

          1)提供1-3个可选按钮来向用户表达信息。这个功能可能和你在任何桌面编程中的经历相似,显示的按钮一般从OK、Cancel、Yes和No中选择。

          2)以checkbox或radio Button的方式提供选项列表。

          3)提供一个供用户输入的文本输入框

本程序Java源码

 


package com.sx.Dialog;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class DialogActivity extends Activity implements android.view.View.OnClickListener 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button btn1 = (Button)findViewById(R.id.btn1);
        final Button btn2 = (Button)findViewById(R.id.btn2);
        final Button btn3 = (Button)findViewById(R.id.btn3);
        final Button btn4 = (Button)findViewById(R.id.btn4);
        final Button btn5 = (Button)findViewById(R.id.btn5);
        final Button btn6 = (Button)findViewById(R.id.btn6);
        final Button btn7 = (Button)findViewById(R.id.btn7);
        final Button btn8 = (Button)findViewById(R.id.btn8);
        
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);
        btn5.setOnClickListener(this);
        btn6.setOnClickListener(this);
        btn7.setOnClickListener(this);
        btn8.setOnClickListener(this);
        
    }
    
    @Override
    public void onClick(View v) 
    {
        switch(v.getId())
        {
        case R.id.btn1:
            dialog1();
            break;
        case R.id.btn2:
            dialog2();
            break;
        case R.id.btn3:
            dialog3();
            break;
        case R.id.btn4:
            dialog4();
            break;
        case R.id.btn5:
            dialog5();
            break;
        case R.id.btn6:
            dialog6();
            break;
        case R.id.btn7:
            dialog7();
            break;
        case R.id.btn8:
            dialog8();
            break;
        }
    }
    
    //1效果: 该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式
     protected void dialog1() 
    {
        AlertDialog.Builder builder = new Builder(DialogActivity.this);
        builder.setMessage("确认退出吗?");

        builder.setTitle("提示");

        builder.setPositiveButton("确认", new OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                   dialog.dismiss();
                   DialogActivity.this.finish();
            }
        });

        builder.setNegativeButton("取消", new OnClickListener() 
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }
    
    //2效果:改变了对话框的图表,添加了三个按钮
     protected void dialog2()
    {
        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) 
                                {
                                    Toast.makeText(DialogActivity.this, "我很喜欢他的电影。",Toast.LENGTH_LONG).show();
                                }
                            })
                            .setNegativeButton("不喜欢", new OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which) 
                                {
                                    Toast.makeText(DialogActivity.this, "我不喜欢他的电影。", Toast.LENGTH_LONG).show();
                                }
                            })
                            .setNeutralButton("一般", new OnClickListener() 
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which) 
                                {
                                    Toast.makeText(DialogActivity.this, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();
                                }
                            }).create();
        dialog.show();
    }
 
    //3效果: 信息内容是一个简单的View类型
    protected void dialog3()
   {
       new AlertDialog.Builder(this)
                    .setTitle("请输入")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setView(new EditText(this))
                    .setPositiveButton("确定", null)
                    .setNegativeButton("取消", null)
                    .show();
    }
 
    //信息内容是一组单选框
     protected void dialog4()
    {
        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();
     }
 
    //信息内容是一组复选框
     protected void dialog5()
    {
        new AlertDialog.Builder(this)
                   .setTitle("复选框")
                   .setMultiChoiceItems(new String[] { "Item1", "Item2" }, null, null)
                   .setPositiveButton("确定", null)
                   .setNegativeButton("取消", null)
                   .show();
    }
 
    //信息内容是一组简单列表
     protected void dialog6()
    {
        new AlertDialog.Builder(this)
                   .setTitle("列表框")
                   .setItems(new String[] { "Item1", "Item2" }, null)
                   .setNegativeButton("确定", null)
                   .show();
    }

    //登录样式
     protected void dialog7()
    {
  
    }
 
    //自定义样式
     protected void dialog8()
    {
        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();
    }

}


 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值