对话框(Dialog)是程序运行过程中弹出的窗口,Android中有好多种对话框,如警告对话框,进度对话框,列表对话框,单选对话框,日期选择对话框,时间选择对话框等;
下面用几个例子来演示一下各种对话框:
(1)在activity_main.xml中添加一个按钮:
<RelativeLayout 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" tools:context="com.xiaozhang.dialog.MainActivity" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button" /> </RelativeLayout>
(2)在strings.xml中加入:
<string name="button">对话框</string>
(3)各种对话框如下:
代码:
package com.xiaozhang.dialog; import java.util.Calendar; import android.app.Activity; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.Gravity; import android.view.View.OnClickListener; import android.widget.TimePicker; import android.widget.Toast; public class View extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(android.view.View v) { Calendar calendar = Calendar.getInstance(); TimePickerDialog.OnTimeSetListener timeListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) { Toast toast = Toast.makeText(View.this, "时间:" + hourOfDay + ":" + minute, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } }; TimePickerDialog dlg = new TimePickerDialog(View.this, timeListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true); dlg.show(); } }); } }
代码如下:
package com.xiaozhang.dialog; import java.util.Calendar; import android.app.Activity; import android.app.DatePickerDialog; import android.os.Bundle; import android.view.Gravity; import android.view.View.OnClickListener; import android.widget.DatePicker; import android.widget.Toast; public class View extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(android.view.View v) { Calendar calendar = Calendar.getInstance(); DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) { Toast toast = Toast.makeText(View.this, "日期:" + year + "年" + (month + 1) + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } }; DatePickerDialog dlg = new DatePickerDialog(View.this, dateListener, calendar.get(Calendar.YEAR), calendar .get(Calendar.MONTH), calendar .get(Calendar.DAY_OF_MONTH)); dlg.show(); } }); } }
代码如下:
package com.xiaozhang.dialog; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View.OnClickListener; import android.view.ViewGroup; public class View extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(android.view.View v) { LayoutInflater inflater = getLayoutInflater(); android.view.View layout = inflater.inflate(R.layout.dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder(View.this).setTitle("自定义对话框") .setView(layout).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } }); } }
dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/icon" android:layout_gravity="center" /> <TextView android:id="@+id/textName1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/textName1" android:textSize="25sp" android:gravity="center"/> <TextView android:id="@+id/textName2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/textName2" android:textSize="25sp" android:gravity="center" /> </LinearLayout>
代码只需要修改onCreate方法中的内容即可:
new AlertDialog.Builder(View.this) .setTitle("复选框") .setMultiChoiceItems( new String[] { "Item1", "Item2", "Item3" }, null, null).setPositiveButton("确定", null) .setNegativeButton("取消", null).show();
new AlertDialog.Builder(View.this).setTitle("请输入") .setIcon(android.R.drawable.ic_dialog_info) .setView(new EditText(View.this)) .setPositiveButton("确定", null) .setNegativeButton("取消", null).show();
代码如下:
new AlertDialog.Builder(View.this) .setTitle("单选框") .setIcon(android.R.drawable.ic_dialog_info) .setSingleChoiceItems( new String[] { "Item1", "Item2" }, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).setNegativeButton("取消", null) .setPositiveButton("确定", null).show();
new AlertDialog.Builder(View.this) .setTitle("列表框") .setItems(new String[] { "Item1", "Item2", "Item3","Item4" }, null) .setNegativeButton("确定", null).show();
package com.xiaozhang.dialog; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { private AlertDialog.Builder builder = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 实例化AlertDialog.Builder对象 builder = new AlertDialog.Builder(this); findViewById(R.id.button).setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener { public void onClick(View v) { builder.setTitle("警告对话框").setMessage("确定要删除该文件吗?"); // 设置确认按钮 builder.setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { show("删除成功"); } }); // 设置取消按钮 builder.setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { show("取消删除"); } }); // 创建对话框 AlertDialog alertDialog = builder.create(); // 显示对话框 alertDialog.show(); } } // 使用Toast来显示点击对话框之后的效果 public void show(String str) { Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } }
package com.xiaozhang.dialog; import android.app.Activity; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class ProcessDialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDialog(0); } }); } @Override protected Dialog onCreateDialog(int id) { // 对进度对话框进行实例化 ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("进度对话框"); dialog.setIndeterminate(true); dialog.setMessage("程序正在Loading..."); dialog.setCancelable(true); dialog.setButton(Dialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show("确定"); } }); return dialog; } // 使用Toast来显示点击对话框之后的效果 public void show(String str) { Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } }
不过showDialog()方法已经不建议使用了;
google推荐的代替方法:
package com.xiaozhang.dialog; import android.app.Activity; import android.app.Dialog; import android.app.DialogFragment; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class ProcessDialog extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyDialogFragment dialog = new MyDialogFragment(); dialog.show(getFragmentManager(), "EditNameDialog"); } }); } class MyDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // 对进度对话框进行实例化 ProgressDialog dialog = new ProgressDialog(ProcessDialog.this); dialog.setTitle("进度对话框"); dialog.setIndeterminate(true); dialog.setMessage("程序正在Loading..."); dialog.setCancelable(true); dialog.setButton(Dialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show("确定"); } }); return dialog; } // 使用Toast来显示点击对话框之后的效果 public void show(String str) { Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } } }
package com.xiaozhang.dialog; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class NaturalDialog extends Activity { private AlertDialog.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); builder = new AlertDialog.Builder(this); findViewById(R.id.button).setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener { @Override public void onClick(View v) { builder.setIcon(R.drawable.icon).setTitle("普通对话框"); builder.setMessage("你擅长Android吗?"); builder.setPositiveButton("擅长", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show("Android:擅长"); } }); builder.setNegativeButton("不擅长", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show("Android:不擅长"); } }); builder.setNeutralButton("一般", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show("Android:一般"); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } } // 使用Toast来显示点击对话框之后的效果 public void show(String str) { Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, toast.getXOffset() / 2, toast.getYOffset() / 2); toast.show(); } }
下面做个例子:
这个例子转载自:http://www.tuicool.com/articles/ZvyuAfU
当点击确定时,转移到登陆对话框,当输入用户名和密码后,转移到登陆进度对话框,然后等待3秒,跳转到另一个页面;
代码如下:
DialogTest.java
package com.xiaozhang.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; public class DialogTest extends Activity { /** Called when the activity is first created. */ ProgressDialog mydialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AlertDialog.Builder builder = new AlertDialog.Builder(DialogTest.this); builder.setTitle("登录提示").setMessage("这里需要登录"); builder.setPositiveButton("确定", // 设置确定按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击确定转向登录对话框 LayoutInflater factory = LayoutInflater .from(DialogTest.this); // 得到自定义对话框 final View DialogView = factory.inflate( R.layout.dialog, null); // 创建对话框 AlertDialog.Builder builder = new AlertDialog.Builder( DialogTest.this); builder.setTitle("登录框").setView(DialogView); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {// 设置监听事件 @Override public void onClick(DialogInterface dialog, int which) { // 输入完成后点击“确定”开始登录 mydialog = ProgressDialog.show( DialogTest.this, "请稍等...", "正在登录...", true); new Thread() { public void run() { try { sleep(3000); } catch (Exception e) { e.printStackTrace(); } finally { // 登录结束,取消mydialog对话框 Intent intent = new Intent(); intent.setClass( DialogTest.this, MainActivity.class); startActivity(intent); } } }.start(); } }); builder.setNegativeButton("取消",// 设置取消按钮 new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击取消后退出程序 DialogTest.this.finish(); } }); builder.create().show(); } }); builder.setNeutralButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击退出后退出程序 DialogTest.this.finish(); } }); builder.create().show(); } }
dialog.xml
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:gravity="start" android:text="用户名" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/username2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:gravity="fill_horizontal" android:inputType="text" android:scrollHorizontally="true" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:gravity="start" android:text="密码" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/password2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:gravity="fill_horizontal" android:inputType="textPassword" android:scrollHorizontally="true" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout>
MainActivity.java
package com.xiaozhang.dialog; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="跳转到这里" /> </LinearLayout>
还要在AndroidManifest.xml中注册这个Activity;
<activity android:name=".DialogTest" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity" android:label="@string/app_name" > </activity>