Android开发中常用的对话框(移动端开发)

82 篇文章 4 订阅 ¥59.90 ¥99.00
本文介绍了Android开发中常用的对话框类型,包括提示对话框、列表对话框、单选对话框和多选对话框,并提供了创建这些对话框的源代码示例,帮助开发者实现与用户的交互。
摘要由CSDN通过智能技术生成

在移动应用程序开发中,对话框是一种常见的用户界面元素,用于显示重要的信息、接收用户输入或确认操作。Android平台提供了多种类型的对话框,每种对话框都有其特定的用途和样式。在本文中,我们将介绍Android开发中常用的几种对话框,并提供相应的源代码示例。

  1. 提示对话框(AlertDialog)

提示对话框是一种常见的对话框类型,用于显示一条消息并等待用户确认。以下是创建一个简单提示对话框的示例代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
package com.tarena.day09_alertdialog; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private AlertDialog dialog1; private AlertDialog dialog2; private AlertDialog dialog3; private AlertDialog dialog4; private AlertDialog dialog5; private ProgressDialog dialog6; private TimePickerDialog dialog7; private DatePickerDialog dialog8; private EditText etUserName, etPassword; private boolean isFlag; private void initTimePickerDialog(){ dialog7 = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, "Time:"+hourOfDay+":"+minute, Toast.LENGTH_LONG).show(); } }, 0, 0, false); } private void initDatePickerDialog(){ dialog8 = new DatePickerDialog(this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, "Date:"+year+":"+(monthOfYear+1)+":"+dayOfMonth, Toast.LENGTH_LONG).show(); } }, 2015, Calendar.SEPTEMBER, 17); } private void initAlertDialog1() { Builder builder = new Builder(this); // AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 创建AlertDialog对象 dialog1 = builder.create(); } private void initAlertDialog2() { Builder builder = new Builder(this); builder.setTitle("提示"); // 添加显示信息 builder.setMessage("最简单对话框"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog2.dismiss(); } }); // 创建AlertDialog对象 dialog2 = builder.create(); } private void initAlertDialog3() { Builder builder = new Builder(this); builder.setTitle("提示"); // 设置控件方法 builder.setView(loadView()); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); // 设置确定按钮 builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = etUserName.getText().toString(); String password = etPassword.getText().toString(); Toast.makeText( MainActivity.this, "UserName:" + username + ",Password:" + password, Toast.LENGTH_SHORT).show(); } }); // 设置取消按钮 builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog3.dismiss(); } }); // 创建AlertDialog对象 dialog3 = builder.create(); } private void initAlertDialog4() { Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setSingleChoiceItems(new String[] { "item1", "item2", "item3", "item4" }, 0, new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Toast.makeText(MainActivity.this, "item1", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "item2", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "item3", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MainActivity.this, "item4", Toast.LENGTH_SHORT).show(); break; default: break; } } }); // 创建AlertDialog对象 dialog4 = builder.create(); } private void initAlertDialog5(){ Builder builder = new Builder(this); builder.setTitle("提示"); // 图标 builder.setIcon(android.R.drawable.dialog_holo_light_frame); builder.setMultiChoiceItems(new String[]{"item1","item2","item3","item4"}, new boolean[]{true,false,true,false}, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this, "Which:"+which+",IsChechked:"+isChecked, Toast.LENGTH_SHORT).show(); } }); dialog5 = builder.create(); } /** * 加载布局 * * @return */ private View loadView() { LayoutInflater inflater = LayoutInflater.from(this); // LayoutInflater inflater = (LayoutInflater) // getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.login, null); etUserName = (EditText) view.findViewById(R.id.et_username); etPassword = (EditText) view.findViewById(R.id.et_password); return view; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initAlertDialog1(); initAlertDialog2(); initAlertDialog3(); initAlertDialog4(); initAlertDialog5(); initTimePickerDialog(); initDatePickerDialog(); addListener(); } private void addListener() { this.findViewById(R.id.button1).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog1.show(); } }); this.findViewById(R.id.button2).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog2.show(); } }); this.findViewById(R.id.button3).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog3.show(); } }); this.findViewById(R.id.button4).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog4.show(); } }); this.findViewById(R.id.button5).setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog5.show(); } }); this.findViewById(R.id.button6).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { isFlag = true; dialog6 = new ProgressDialog(MainActivity.this); dialog6.setTitle("提示"); dialog6.setIcon(R.drawable.ic_launcher); dialog6.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog6.setProgress(100); dialog6.setCancelable(false); dialog6.setButton("完成", new android.content.DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { isFlag = false; dialog6.dismiss(); } }); dialog6.show(); new Thread(){ public void run() { int number = 0; while (isFlag) { dialog6.setProgress(number); number++; if(number > 100){ isFlag = false; dialog6.cancel(); } try { Thread.sleep(80); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; }.start(); } }); this.findViewById(R.id.button7).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog7.show(); } }); this.findViewById(R.id.button8).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog8.show(); } }); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值