`
阅读更多

--------------------------------------------Layout 输入文本对话框-------------------------------

<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>

--------------------------------------------MainActivity.java------------------------------------

package com.ch06;

 

import android.app.Activity;

import android.app.AlertDialog;

import android.app.Dialog;

import android.app.ProgressDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.widget.ImageView;

 

/**

 * 

 * 项目名称:com.ch06    

 * 类名称:MainActivity    

 * 类描述: 普通对话框、单选按钮项对话框、多选按钮项对话框、输入文本对话框、进度对话框

 * 创建人:fy   

 * 创建时间:2012-11-8 下午12:16:15   

 * Copyright (c) 方勇-版权所有

 */

public class MainActivity extends Activity {

 

private final static int RADIO_CHECKED_Enu = 0;// 大学

private final static int RADIO_CHECKED_Sel = 1;// 高中

/* 复选按钮状态为全选 */

private boolean[] checked = { truetruetrue };

/* 模拟的进度值 */

private int progressNum;

/* 进度对话框 */

private ProgressDialog progressDialog;

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

buildAlertDialog().show();

buildAlertDialog_radio().show();

buildAlertDialog_checkbox().show();

buildAlertDialog_input().show();

buildAlertDialog_progress().show();

updateProgress();

}

 

/* 普通对话框 */

private Dialog buildAlertDialog() {

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

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

builder.setMessage("您的密码不对!!");

 

ImageView imageView = new ImageView(this);

imageView.setImageResource(R.drawable.mm1);

// 背景图片

builder.setView(imageView);

 

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

@Override

public void onClick(DialogInterface dialog, int which) {

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

}

});

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

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

setTitle("您点击的是详情按钮!");

}

});

 

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

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

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

}

});

return builder.create();

}

 

/* 单选按钮弹出框 */

private Dialog buildAlertDialog_radio() {

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

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

// builder.setMessage("您的密码不对!!");

// 单选按钮,高中被选中

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

 

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

switch (which) {

case RADIO_CHECKED_Enu:

setTitle("大学");

break;

case RADIO_CHECKED_Sel:

setTitle("高中");

break;

default:

setTitle("初中");

break;

}

}

});

 

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

@Override

public void onClick(DialogInterface dialog, int which) {

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

}

});

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

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

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

}

});

return builder.create();

}

 

/* 多选按钮弹出框 */

private Dialog buildAlertDialog_checkbox() {

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

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

// builder.setMessage("您的密码不对!!");

 

// 复选按钮

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

new DialogInterface.OnMultiChoiceClickListener() {

 

@Override

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

// TODO Auto-generated method stub

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

}

});

 

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

@Override

public void onClick(DialogInterface dialog, int which) {

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

}

});

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

@Override

public void onClick(DialogInterface dialog, int which) {

// TODO Auto-generated method stub

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

}

});

return builder.create();

}

 

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

private Dialog buildAlertDialog_input() {

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

builder.setIcon(R.drawable.ic_launcher);

builder.setTitle("对话框");

// builder.setMessage("您的密码不对!!");

// 文本框

LayoutInflater inflater = LayoutInflater.from(this);

 

builder.setView(inflater.inflate(R.layout.activity_inputnull));

 

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

@Override

public void onClick(DialogInterface dialog, int which) {

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

}

});

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

@Override

public void onClick(DialogInterface dialog, int which) {

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

}

});

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.1秒更新一次进度 */

public void updateProgress() {

new Thread() {

@Override

public void run() {

try {

while (progressNum <= 100) {

progressDialog.setProgress(progressNum++);

Thread.sleep(100);

super.run();

}

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

progressDialog.cancel();

catch (InterruptedException e) {

e.printStackTrace();

}

}

}.start();

}

}

--------------------------------------------效果 普通对话框-------------------------------------

--------------------------------------------效果 单选按钮对话框---------------------------------

--------------------------------------------效果 多选按钮对话框---------------------------------

 

--------------------------------------------效果 输入对话框-------------------------------------

 

--------------------------------------------效果 进度条对话框-----------------------------------

 

<!--EndFragment-->
  • 大小: 127.1 KB
  • 大小: 97.7 KB
  • 大小: 96.6 KB
  • 大小: 102.7 KB
  • 大小: 104.1 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics