企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
感觉对Dialog了解很少,今天看书学习一下,顺便更新一下blog(吐槽一下,最近几天都很忙没上blog)。。 1)自定义Dialog layout ![](https://box.kancloud.cn/2016-06-13_575e8e163c3b1.jpg) 上面的Dialog界面采用了自己的layout,下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.Dialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub final Dialog dialog = new Dialog(MainActivity.this); dialog.setTitle("标题"); dialog.setContentView(R.layout.dialog_layout); dialog.show(); Button button = (Button) dialog.findViewById(R.id.bt); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub dialog.dismiss(); } }); } }); } @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; } } ~~~ 下面是自定义的layout代码: ~~~ <?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:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DIALOG" /> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> ~~~ 2)AlertDialog AlertDialog是常用的Dialog实现之一,要构造AlertDialog界面,需要用到一个新的AlertDialog.Buider对象。 下面是实现界面: ![](https://box.kancloud.cn/2016-06-13_575e8e1661a9a.jpg) 下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); dialog.setTitle("标题"); dialog.setMessage("message"); dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "点击了确认", 0).show(); } }); dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); dialog.show(); } }); } @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; } } ~~~ 点击确认和取消按钮都有相应的响应事件。 需要注意的是,点击一次按钮在响应事件之后都会关闭对话框。在上面按下返回键不会关闭对话框的,如果想要返回键关闭对话框可以使用setOnCancleListener方法附上一个OnCancelListener来响应 3)DataPickerDialog 用户用这个Dialog选择时间,构造函数中有一个onDateSet函数通知Activity日期已经被设定 ![](https://box.kancloud.cn/2016-06-13_575e8e169301d.jpg) 下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub DatePickerDialog dialog = new DatePickerDialog( MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Toast.makeText( MainActivity.this, year + "年" + monthOfYear + "月" + dayOfMonth + "日", 0).show(); } // 注意:月份是从0开始的,所以要-1 }, 2014, 7, 19); dialog.show(); } }); } @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; } } ~~~ 4)TimePickerDialog TimePickerDialog的用法和上面的差不多,只不过这个是设置时间的上面是设置日期的。 ![](https://box.kancloud.cn/2016-06-13_575e8e16b3663.jpg) 下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, hourOfDay+"分"+minute+"秒", 0).show(); } }, 12, 12, true); dialog.setTitle("时间"); dialog.show(); } }); } @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; } } ~~~ 5)AlertDialog中的setView 前面忘记说一点了,通过setView函数可以在AlertDialog中添加View,比如EditText ,TextView等等的,这样定制性会很高,又符合Dialog简单窗口的特性。 ![](https://box.kancloud.cn/2016-06-13_575e8e16d6f5b.jpg) 下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new AlertDialog.Builder(MainActivity.this).setTitle("titile") .setView(new EditText(MainActivity.this)).setPositiveButton("确认", new AlertDialog.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "选择了确定", 0).show(); } }).show(); } }); } @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; } } ~~~ 6)ProgressDialog ProgressDialog是用来显示进度条的,默认分为圆形和水平两种,下面演示水平的。 ![](https://box.kancloud.cn/2016-06-13_575e918df08a4.jpg) 下面是代码: ~~~ package com.example.androiddialog; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub ProgressDialog dialog = new ProgressDialog(MainActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setTitle("title"); dialog.setMessage("message"); // 设置点击其他部分会不会取消Dialog dialog.setCanceledOnTouchOutside(false); // 返回键能否取消 dialog.setCancelable(false); dialog.setMax(50); dialog.incrementProgressBy(20); dialog.incrementProgressBy(2); dialog.show(); } }); } @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; } } ~~~