对话框AlertDialog。API:
使用AlertDialog.Builder创建对话框需要了解以下几个方法: [li]setTitle :为对话框设置标题[/li][li]setIcon :为对话框设置图标[/li][li]setMessage:为对话框设置内容[/li][li]setView : 给对话框设置自定义样式[/li][li]setItems :设置对话框要显示的一个list,一般用于显示几个命令时。[/li][li]setMultiChoiceItems :用来设置对话框显示一系列的复选框。[/li][li]setNeutralButton :[/li][li]setPositiveButton :给对话框添加"Yes"按钮[/li][li]setNegativeButton :对话框添加"No"按钮[/li][li]create : 创建对话框[/li][li]show :显示对话框
[/li] 下面我们来看一下最简单对话框。
这个对话框只是简单的显示内容,使用默认图标,没有按钮,不多说,贴代码:
[li]new AlertDialog.Builder(Lesson_01_Pic.this).setTitle("提示标题").setMessage("这是提示内容").show();
[/li]
复制代码
(Lesson_02_Dia是类名,请换成自己的!!)下面我们为这个对话框加个按钮,效果: 代码:[li] new AlertDialog.Builder(Lesson_01_Pic.this)[/li][li] .setTitle("这是标题")[/li][li] .setMessage("这是提示内容")[/li][li] .setPositiveButton("确定",[/li][li] new DialogInterface.OnClickListener(){[/li][li] public void onClick(DialogInterface dialoginterface, int i){[/li][li] //按钮事件[/li][li] Toast.makeText(Lesson_01_Pic.this, "确定",Toast.LENGTH_LONG).show();[/li][li] }[/li][li] }).show();
[/li]
复制代码
添加按钮时,需要同时为该按钮指定监听器。下面,我们修改一个图标,添加两个按钮,同时显示多个选项,先看下效果: 代码:[li]package com.yfz;[/li][li]import android.app.Activity;[/li][li]import android.app.AlertDialog;[/li][li]import android.app.Dialog;[/li][li]import android.content.DialogInterface;[/li][li]import android.content.DialogInterface.OnClickListener;[/li][li]import android.content.DialogInterface.OnMultiChoiceClickListener;[/li][li]import android.os.Bundle;[/li][li]import android.view.View;[/li][li]import android.widget.Button;[/li][li]import android.widget.Toast;[/li][li]public class Lesson_02_Dia extends Activity {[/li][li] /** Called when the activity is first created. */[/li][li] @Override[/li][li] public void onCreate(Bundle savedInstanceState) {[/li][li] super.onCreate(savedInstanceState);[/li][li] setContentView(R.layout.main);[/li][li][/li][li] Button button = (Button)findViewById(R.id.b01);[/li][li] button.setText("对话框");[/li][li] button.setOnClickListener(new Button.OnClickListener(){[/li][li] @Override[/li][li] public void onClick(View v) {[/li][li] //选项数组[/li][li] String[] choices={"Facebook","Twitter"};[/li][li] //Check判断数组,与选项对应[/li][li] boolean[] chsBool = {true,false};[/li][li] //包含多个选项及复选框的对话框[/li][li] AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)[/li][li] .setIcon(android.R.drawable.btn_star_big_on)[/li][li] .setTitle("调查")[/li][li] .setMultiChoiceItems(choices, chsBool, multiClick)[/li][li] .setPositiveButton("Yes", onclick)[/li][li] .setNegativeButton("No", onclick).create();[/li][li] dialog.show();[/li][li] }[/li][li][/li][li] });[/li][li] }[/li][li][/li][li] /**[/li][li] * 对话框复选框事件监听器[/li][li] */[/li][li] OnMultiChoiceClickListener multiClick = new OnMultiChoiceClickListener(){[/li][li] @Override[/li][li] public void onClick(DialogInterface dialog, int which, boolean isChecked) {[/li][li] Toast.makeText(Lesson_02_Dia.this, "第"+(which+1)+"项,选中结果:"+isChecked,Toast.LENGTH_SHORT).show();[/li][li] }[/li][li][/li][li] };[/li][li][/li][li] /**[/li][li] * 对话框按钮点击事件监听器[/li][li] */[/li][li] OnClickListener onclick = new OnClickListener() {[/li][li] @Override[/li][li] public void onClick(DialogInterface dialog, int which) {[/li][li] switch (which) {[/li][li] case Dialog.BUTTON_NEGATIVE:[/li][li] Toast.makeText(Lesson_02_Dia.this, "No..",[/li][li] Toast.LENGTH_LONG).show();[/li][li] break;[/li][li] case Dialog.BUTTON_NEUTRAL:[/li][li] Toast.makeText(Lesson_02_Dia.this, "I don't know.",[/li][li] Toast.LENGTH_LONG).show();[/li][li] break;[/li][li] case Dialog.BUTTON_POSITIVE:[/li][li] Toast.makeText(Lesson_02_Dia.this, "Yes!!",[/li][li] Toast.LENGTH_LONG).show();[/li][li] break;[/li][li] }[/li][li] }[/li][li] };[/li][li]}
[/li]
复制代码
说明已经写在注释中了。下面再介绍一种比较常用的式样,如图: 代码:[li] @Override[/li][li] public void onCreate(Bundle savedInstanceState) {[/li][li] super.onCreate(savedInstanceState);[/li][li] setContentView(R.layout.main);[/li][li][/li][li] Button button = (Button)findViewById(R.id.b01);[/li][li] button.setText("对话框");[/li][li] button.setOnClickListener(new Button.OnClickListener(){[/li][li] @Override[/li][li] public void onClick(View v) {[/li][li] //选项数组[/li][li] String[] choices={"新浪微博","校内","街旁"};[/li][li] //包含多个选项的对话框[/li][li] AlertDialog dialog = new AlertDialog.Builder(Lesson_02_Dia.this)[/li][li] .setIcon(android.R.drawable.btn_star)[/li][li] .setTitle("分享")[/li][li] .setItems(choices, onselect).create();[/li][li] dialog.show();[/li][li] }[/li][li] });[/li][li] }[/li][li][/li][li][/li][li] /**[/li][li] * 选项的事件监听器[/li][li] */[/li][li] OnClickListener onselect = new OnClickListener() {[/li][li] @Override[/li][li] public void onClick(DialogInterface dialog, int which) {[/li][li] // TODO Auto-generated method stub[/li][li] switch (which) {[/li][li] case 0:[/li][li] Toast.makeText(Lesson_02_Dia.this, "您选择了新浪微博.",Toast.LENGTH_SHORT).show();[/li][li] break;[/li][li] case 1:[/li][li] Toast.makeText(Lesson_02_Dia.this, "您选择了校内",Toast.LENGTH_SHORT).show();[/li][li] break;[/li][li] case 2:[/li][li] Toast.makeText(Lesson_02_Dia.this, "您选择了街旁",Toast.LENGTH_SHORT).show();[/li][li] break;[/li][li] }[/li][li] }[/li][li][/li][li] };
[/li]
复制代码
好了,今天就写到这,改天写一下,如果在弹出框中做一个登陆界面。继续补充...先上图... 页面login.xml: 示例写的比较简单,布局大家可以自己完善、修改。[li]<?xml version="1.0" encoding="utf-8"?>[/li][li]<TableLayout[/li][li]android:id="@+id/widget36"[/li][li]android:layout_width="fill_parent"[/li][li]android:layout_height="fill_parent"[/li][li]androidrientation="vertical"[/li][li]xmlns:android="http://schemas.android.com/apk/res/android"[/li][li]>[/li][li]<TextView[/li][li]android:id="@+id/widget37"[/li][li]android:layout_width="wrap_content"[/li][li]android:layout_height="wrap_content"[/li][li]android:text="用户名:"[/li][li]>[/li][li]</TextView>[/li][li]<EditText[/li][li]android:id="@+id/widget38"[/li][li]android:layout_width="wrap_content"[/li][li]android:layout_height="wrap_content"[/li][li]android:text=""[/li][li]android:textSize="18sp"[/li][li]>[/li][li]</EditText>[/li][li]<TextView[/li][li]android:id="@+id/widget39"[/li][li]android:layout_width="wrap_content"[/li][li]android:layout_height="wrap_content"[/li][li]android:text="密码:"[/li][li]>[/li][li]</TextView>[/li][li]<EditText[/li][li]android:id="@+id/widget40"[/li][li]android:layout_width="wrap_content"[/li][li]android:layout_height="wrap_content"[/li][li]android:text=""[/li][li]android:textSize="18sp"[/li][li]>[/li][li]</EditText>[/li][li]</TableLayout>
[/li]
复制代码
代码 : (也比较简单)[li] LayoutInflater factory = LayoutInflater.from(Lesson_02_Dia.this);[/li][li] //获得自定义对话框[/li][li] View view = factory.inflate(R.layout.login, null);[/li][li][/li][li] AlertDialog dialog02 = new AlertDialog.Builder(Lesson_02_Dia.this)[/li][li] .setIcon(android.R.drawable.btn_star)[/li][li] .setTitle("登录")[/li][li] .setView(view)[/li][li] .setPositiveButton("Yes", onclick)[/li][li] .setNegativeButton("No", onclick).create();[/li][li] dialog02.show();
[/li] |