一个Android开发中经常使用对话框的小样例,共同拥有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框:

<LinearLayout 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:orientation="vertical" >





    <Button

        android:id="@+id/common_dialog"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:text="普通对话框"

        android:textSize="16sp"

        android:layout_marginTop="10dp" />





    <Button

        android:id="@+id/radio_dialog"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:text="单选对话框"

        android:textSize="16sp"

        android:layout_marginTop="10dp"  />





    <Button

        android:id="@+id/check_dialog"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:text="多选对话框" 

        android:textSize="16sp"

        android:layout_marginTop="10dp" />





    <Button

        android:id="@+id/input_dialog"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:text="输入文字对话框" 

        android:textSize="16sp"

        android:layout_marginTop="10dp" />





    <Button

        android:id="@+id/progress_dialog"

        android:layout_width="match_parent"

        android:layout_height="40dp"

        android:text="进度条对话框" 

        android:textSize="16sp"

        android:layout_marginTop="10dp" />





</LinearLayout>

以下是输入内容的简单布局activity_input.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >





    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />





    <EditText

        android:id="@+id/uname"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />





    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />





    <EditText

        android:id="@+id/upass"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />





</LinearLayout>

代码及凝视:

public class MainActivity extends Activity implements OnClickListener {

/**单选框模拟标题 大学*/

private final static int CHECKED_ENU = 0;

/**单选框模拟标题  高中*/

private final static int CHECKED_SEL = 1;

/**单选框模拟标题  初中*/

private final static int CHECKED_CHU = 2;

/**复选button状态为全选 */

private boolean[] checked = { true, true, true, false };

/**模拟的进度值 */

private int progressNumber;

/**进度对话框 */

private ProgressDialog progressDialog;

/**相应button*/

private Button commonBtn, radioBtn, checkBtn, inputBtn, progressBtn;





@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initViews();

initListeners();

}





/**初始化UI控件*/





private void initViews() {

this.commonBtn = (Button) findViewById(R.id.common_dialog);

this.radioBtn = (Button) findViewById(R.id.radio_dialog);

this.checkBtn = (Button) findViewById(R.id.check_dialog);

this.inputBtn = (Button) findViewById(R.id.input_dialog);

this.progressBtn = (Button) findViewById(R.id.progress_dialog);

}





/**注冊button监听事件*/

private void initListeners() {

this.commonBtn.setOnClickListener(this);

this.radioBtn.setOnClickListener(this);

this.checkBtn.setOnClickListener(this);

this.inputBtn.setOnClickListener(this);

this.progressBtn.setOnClickListener(this);

}





/**普通对话框 */

private Dialog buildAlertDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

builder.setMessage("您的password不正确!!");





ImageView imageView = new ImageView(this);

imageView.setImageResource(R.drawable.mm1);

/**设置背景图片*/

builder.setView(imageView);

/**左边button*/

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是左边确定button!");

}

});

/**中间button*/

builder.setNeutralButton("详情", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是中间详情button!");

}

});

/**右边button*/

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

setTitle("您点击的是右边取消button!");

}

});

return builder.create();

}





/**单选button弹出框 */

private Dialog buildAlertDialog_radio() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

/**单选button,默认高中被选中*/

builder.setSingleChoiceItems(new String[] { "大学", "高中", "初中", "小学" }, 1, new DialogInterface.OnClickListener() {





@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

switch (which) {

case CHECKED_ENU:

setTitle("大学");

break;

case CHECKED_SEL:

setTitle("高中");

break;

case CHECKED_CHU:

setTitle("初中");

break;

default:

setTitle("小学");

break;

}

}

});





builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是左边确定button!");

}

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是右边取消button!");

}

});

return builder.create();

}





/**能够多选button弹出框 */

private Dialog buildAlertDialog_checkbox() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

/**复选button*/

builder.setMultiChoiceItems(new String[] { "大学", "高中", "初中", "小学" }, checked, new DialogInterface.OnMultiChoiceClickListener() {





@Override

public void onClick(DialogInterface dialog, int which, boolean isChecked) {

setTitle("which=" + which + "-----" + "isChecked=" + isChecked);

}

});





builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击了确定button!");

}

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

setTitle("您点击的是了取消button!");

}

});

return builder.create();

}





/**含能够输入文本的弹出框 */

private Dialog buildAlertDialog_input() {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

LayoutInflater inflater = LayoutInflater.from(this);

builder.setView(inflater.inflate(R.layout.activity_input, null));

builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是确定button!");

}

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

setTitle("您点击的是取消button!");

}

});

return builder.create();

}





/**进度对话框 */

private Dialog buildAlertDialog_progress() {

progressDialog = new ProgressDialog(this);

progressDialog.setTitle("进度条");

progressDialog.setMessage("正在下载...........");

/**进度条样式 */

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

/**模糊效果 */

progressDialog.setIndeterminate(false);

return progressDialog;

}





/**每隔0.3秒更新一次进度 */

public void updateProgress() {

new Thread() {

@Override

public void run() {

try {

while (progressNumber <= 100) {

progressDialog.setProgress(progressNumber++);

Thread.sleep(300);

super.run();

}

/**下载完后,关闭下载框 */

progressDialog.cancel();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

}





@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.common_dialog:

buildAlertDialog().show();

break;

case R.id.radio_dialog:

buildAlertDialog_radio().show();

break;

case R.id.check_dialog:

buildAlertDialog_checkbox().show();

break;

case R.id.input_dialog:

buildAlertDialog_input().show();

break;

case R.id.progress_dialog:

buildAlertDialog_progress().show();

updateProgress();

break;

default:

break;

}

}

}

Android经常使用的五种弹出对话框的更多相关文章

  1. 【转】javascript入门系列演示·三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  2. 【JSP】三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  3. JavaScript中的三种弹出对话框

    学习过js的小伙伴会发现,我们在一些实例中用到了alert()方法.prompt()方法.prompt()方法,他们都是在屏幕上弹出一个对话框,并且在上面显示括号内的内容,使用这种方法使得页面的交互性 ...

  4. javascript入门系列演示·三种弹出对话框的用法实例

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  5. JavaScript:九种弹出对话框

    [1.最基本的js弹出对话框窗口代码] 这是最基本的js弹出对话框,其实代码就几句非常简单: <script LANGUAGE="javascript"> <!- ...

  6. JSP中三种弹出对话框的用法《转》

    对话框有三种 1:只是提醒,不能对脚本产生任何改变: 2:一般用于确认,返回 true 或者 false ,所以可以轻松用于 if...else...判断 3: 一个带输入的对话框,可以返回用户填入的 ...

  7. javascript 三种弹出对话框

    第一种:alert()方法 第二种:confirm()方法 返回一个布尔值,根据返回的值可以执行相应操作. 第三种: prompt()方法 返回输入的消息,或者其默认值提示框经常用于提示用户在进入页面 ...

  8. (转)winform(C#)里几种弹出对话框

    //消息框中需要显示哪些按钮,此处显示“确定”和“取消” MessageBoxButtons messButton = MessageBoxButtons.OKCancel; //"确定要退 ...

  9. js中三种弹出框

    javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prompt()来获得,可以利用这些对话框来完成js的输入和输出,实现与用户能进行交互的js代码 ...

随机推荐

  1. CSS3 边框

    说明:CSS3完全向后兼容,因此不必改变现有的设计.浏览器通常支持CSS2 CSS3模块 CSS3被划分为模块: 选择器 框模型 背景和边框 文本效果 2D/3D 转换 动画 多列布局 用户界面 CS ...

  2. Android开发手记(11) 滑动条SeekBar

    安卓滑动条的操作特别简单,通过getProgress()可以获得SeekBar的位置,通过setProgress(int progress)可以设置SeekBar的位置.要想动态获取用户对SeekBa ...

  3. iOS9中通过UIStackView实现类似大众点评中的效果图

    效果图如下: 实现思路 整体可以看做为一个大的UIStackView(排列方式水平)包括一个子UIStackView(排列方式垂直),其中左边包括一个图片,右边的UIStackView中可以看做包括三 ...

  4. C++拾遗(十三)友元和嵌套类

    友元类 使用友元的场合: 1.两个类既不是is-a关系也不是has-a关系,但是两个类之间又需要有联系,且一个类能访问另一个类的私有成员和保护成员. 2.一个类需要用到另外多个类的私有成员. C++p ...

  5. static和const关键字的作用

    static关键字至少有下列n个作用: (1)函数体内static变量的作用范围为该函数体,不同于auto变量,该变量的内存只被分配一次,因此其值在下次调用时仍维持上次的值: (2)在模块内的stat ...

  6. Ubuntu最小化桌面快捷键Super+D不生效解决

    之前用的Debian,最近研发老大让统一使用Ubuntu来开发,安装了Ubuntu 15.10之后,设置了最小化桌面的快捷键为Super+D(在Systm Settings/系统设置—>Keyb ...

  7. javascript之闭包深入理解(一)

    曾经在开始学习javascript的时候,很是不理解闭包的概念.今天想对它详细的剖析. 在说清楚闭包之前,必须先清楚作用域链. 作用域链 我们知道,执行环境是js中最为重要的一个概念.执行环境定义了变 ...

  8. smarty练习:考试系统

    考试系统 (0607) 做一个类似于驾校考试的系统,可以选择要考试试题类型,选好后进入考试页面 使用的数据库表格:timu(题目)表,xuanxiang(选项)表,shiti(试题)表,shititi ...

  9. ubuntu 12.04 安装nginx

    nginx是一个强大的高性能反向代理服务器,其特点是占内存少,并发能力强,在高并发的情况下是apache 的一个不错的代替品,能够支持高达50000个并发连接数的响应,那么这么强大的武器,究竟如何在l ...

  10. salt-grains

    自定义grains 的方法 1: 在minion 的配置文件夹 /etc/salt/minion.d  下面包含的配置文件  grains.conf 2: 在salt的安装目录中建立grains 文件 ...