您的位置:首页 > 移动开发 > Android开发

android Dialog对话框使用示例

2015-12-14 00:00 579 查看
ch7_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是一个对话框的示例"/>
<Button android:id="@+id/bt_showCommonDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示普通对话框"/>

<Button android:id="@+id/bt_showButtonDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示带按钮的对话框"/>

<Button android:id="@+id/bt_showInputDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示输入对话框"/>

<Button android:id="@+id/bt_showListDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示列表对话框"/>

<Button android:id="@+id/bt_showRadioDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示单选按钮对话框"/>

<Button android:id="@+id/bt_showCheckBoxDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示复选框对话框"/>

<Button android:id="@+id/bt_showDatetimePcikDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示日期时间对话框"/>

<Button android:id="@+id/bt_showProgressDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示进度条对话框"/>

<Button android:id="@+id/bt_showMyDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示自定义对话框"/>

</LinearLayout>


ch7_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="用户名: "/>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>

</LinearLayout>

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密  码: "/>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>


DialogActivity.java:

package com.example.ch7;

import java.util.Calendar;

import com.example.baseexample.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DialogActivity extends Activity{

private Button bt_showCommonDialog;
private Button bt_showButtonDialog;
private Button bt_showInputDialog;
private Button bt_showListDialog;
private Button bt_showRadioDialog;
private Button bt_showCheckBoxDialog;
private Button bt_showDatetimePcikDialog;
private Button bt_showProgressDialog;
private Button bt_showMyDialog;

final String[] arrayHobby = {"篮球","足球","羽毛球","乒乓球"};

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ch7_dialog);
bt_showCommonDialog = (Button)findViewById(R.id.bt_showCommonDialog);
bt_showButtonDialog = (Button)findViewById(R.id.bt_showButtonDialog);
bt_showInputDialog = (Button)findViewById(R.id.bt_showInputDialog);
bt_showListDialog = (Button)findViewById(R.id.bt_showListDialog);
bt_showRadioDialog = (Button)findViewById(R.id.bt_showRadioDialog);
bt_showCheckBoxDialog = (Button)findViewById(R.id.bt_showCheckBoxDialog);
bt_showDatetimePcikDialog = (Button)findViewById(R.id.bt_showDatetimePcikDialog);
bt_showProgressDialog = (Button)findViewById(R.id.bt_showProgressDialog);
bt_showMyDialog = (Button)findViewById(R.id.bt_showMyDialog);

bt_showCommonDialog.setOnClickListener(new BtClickListener());
bt_showButtonDialog.setOnClickListener(new BtClickListener());
bt_showInputDialog.setOnClickListener(new BtClickListener());
bt_showListDialog.setOnClickListener(new BtClickListener());
bt_showRadioDialog.setOnClickListener(new BtClickListener());
bt_showCheckBoxDialog.setOnClickListener(new BtClickListener());
bt_showDatetimePcikDialog.setOnClickListener(new BtClickListener());
bt_showProgressDialog.setOnClickListener(new BtClickListener());
bt_showMyDialog.setOnClickListener(new BtClickListener());

}

class BtClickListener implements OnClickListener{

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bt_showCommonDialog:showDialog(1);break;
case R.id.bt_showButtonDialog:showDialog(2);break;
case R.id.bt_showInputDialog:showDialog(3);break;
case R.id.bt_showListDialog:showDialog(4);break;
case R.id.bt_showRadioDialog:showDialog(5);break;
case R.id.bt_showCheckBoxDialog:showDialog(6);break;
case R.id.bt_showDatetimePcikDialog:showDialog(7);break;
case R.id.bt_showProgressDialog:showDialog(8);break;
case R.id.bt_showMyDialog:showDialog(9);break;
}

}

}

protected Dialog onCreateDialog(int id){
Dialog alertDialog = null;
switch(id){
case 1:
alertDialog = new AlertDialog.Builder(this).setTitle("普通对话框").setMessage("这是一个普通对话框").setIcon(R.drawable.ic_launcher).create();
break;
case 2:
alertDialog = new AlertDialog.Builder(this).setTitle("确定退出?").setMessage("您确定退出程序?").setIcon(R.drawable.ic_launcher).setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
finish();

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

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

}
}).create();
break;
case 3:
alertDialog = new AlertDialog.Builder(this).setTitle("输入").setIcon(R.drawable.ic_launcher).setView(new EditText(this)).setPositiveButton("确定", null).setNegativeButton("取消", null).create();
break;
case 4:
alertDialog = new AlertDialog.Builder(this).setTitle("运动列表").setIcon(R.drawable.ic_launcher).setItems(arrayHobby, null).setPositiveButton("确定", null).setNegativeButton("取消", null).create();
break;
case 5:
alertDialog = new AlertDialog.Builder(this).setTitle("你喜欢哪种运动").setIcon(R.drawable.ic_launcher).setSingleChoiceItems(arrayHobby, 0, null).setPositiveButton("确认", null).setNegativeButton("取消", null).create();
break;
case 6:
alertDialog = new AlertDialog.Builder(this).setTitle("你喜欢哪些运动").setIcon(R.drawable.ic_launcher).setMultiChoiceItems(arrayHobby, null, null).setPositiveButton("确认", null).setNegativeButton("取消", null).create();
break;
case 7:
Calendar c = Calendar.getInstance();
alertDialog = new DatePickerDialog(this,null,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
break;
case 8:
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("下载进度");
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setProgress(10);
pd.setCancelable(true);
alertDialog = (Dialog)pd;
break;
case 9:
LayoutInflater layoutInflater = LayoutInflater.from(this);
View loginView = layoutInflater.inflate(R.layout.ch7_login, null);
alertDialog = new AlertDialog.Builder(this).setTitle("用户登录").setIcon(R.drawable.ic_launcher).setView(loginView).setPositiveButton("登录", null).setNegativeButton("取消", null).create();
break;
}
return alertDialog;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: