Dialog:对话框,对话框有很多种,系统有已经定义好的Dialog,如前面讲的TimePicker和DatePicker都是系统封装好的,可以直接调用就可以的。现在我们来自己定义自己的Dialog。
效果:
开始定义的两个按钮
Dialog1,这个对话框只定义了一个按钮,没有设置取消按钮
Dialog2,这个对话框设置了取消按钮
layout中的xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="DialogActivatyTest" />
- <Button
- android:id="@+id/alert1Dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Alert1Dialog"
- android:textSize="18dp"/>
- <Button
- android:id="@+id/alert2Dialog"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Alert2Dialog"
- android:textSize="18dp"/>
- </LinearLayout>
Activaty.java文件
- package com.cheng.dialogproject;
- import android.os.Bundle;
- import android.R.integer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class DialogActivity extends Activity {
- //定义两个按钮,分别启动不同格式的Dialog
- private Button alert1Button ,alert2bButton;
- //设置两种Dialog的id
- private static final int ALERT_DIALOG_1 = 1;
- private static final int ALERT_DIALOG_2 = 2;
- protected Dialog onCreateDialog(int id) {
- Dialog mdiaDialog;
- AlertDialog.Builder mBuilder;//用来建设自定义的Dialog
- switch (id) {
- case ALERT_DIALOG_1:
- //初始化Builder
- mBuilder = new AlertDialog.Builder(DialogActivity.this);
- //setMessage设置Dialog的标题
- //setcancelable设置按回车键是否退出对话框
- //setPositiveButton设置左边的按钮
- mBuilder.setMessage("自己定义的对话框1怎么样?").setCancelable(false)
- .setPositiveButton("好", new OnClickListener() {
- //设置左边按钮的监听器
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(DialogActivity.this, "你点击了好!", Toast.LENGTH_LONG).show();
- }
- });
- //创建Dialog
- mdiaDialog = mBuilder.create();
- break;
- case ALERT_DIALOG_2:
- mBuilder = new AlertDialog.Builder(DialogActivity.this);
- //设置按返回键是否能推出对话框
- mBuilder.setCancelable(false);
- //setNegativeButton设置取消按钮
- mBuilder.setMessage("自己定义的对话框2比对话框1好?")
- .setPositiveButton("是的", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(DialogActivity.this, "你点击了是!", Toast.LENGTH_LONG).show();
- }
- }).setNegativeButton("不是", new OnClickListener() {
- //设置取消的监听器
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- })
- ;
- mdiaDialog = mBuilder.create();
- break;
- default:
- mdiaDialog = null;
- break;
- }
- return mdiaDialog;
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.main);
- alert1Button = (Button)findViewById(R.id.alert1Dialog);
- alert2bButton = (Button)findViewById(R.id.alert2Dialog);
- View.OnClickListener ocl= new View.OnClickListener() {
- //设置监听器,通过Dialog的id来显示我们点击的Dialog
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.alert1Dialog:
- //显示对话框
- showDialog(ALERT_DIALOG_1);
- break;
- case R.id.alert2Dialog:
- showDialog(ALERT_DIALOG_2);
- break;
- default:
- break;
- }
- }
- };
- //绑定监听器
- alert1Button.setOnClickListener(ocl);
- alert2bButton.setOnClickListener(ocl);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
源代码下载: