从零开始学android<Dialog对话框.二十五.>
1
|
public void setTitle(CharSequence title)
|
普通
|
设置对话框的显示标题
|
2
|
public void setTitle(int titleId)
|
普通
|
设置对话框的显示标题,内容为资源文件指定
|
3
|
public void show()
|
普通
|
显示对话框
|
4
|
public void hide()
|
普通
|
隐藏对话框
|
5
|
public boolean isShowing()
|
普通
|
判断对话框是否显示
|
6
|
public void setContentView(View view)
|
普通
|
设置View组件
|
7
|
public void setContentView(int layoutResID)
|
普通
|
设置View组件的ID
|
8
|
public void dismiss()
|
普通
|
隐藏对话框
|
9
|
public void closeOptionsMenu()
|
普通
|
关闭选项菜单
|
10
|
public void setDismissMessage(Message msg)
|
普通
|
设置隐藏对话框时的消息
|
11
|
public void setCancelable(boolean flag)
|
普通
|
设置是否可以取消
|
12
|
public void setCancelMessage(Message msg)
|
普通
|
设置对话框取消时的消息
|
13
|
public void cancel()
|
普通
|
取消对话框,与dismiss()方法类似
|
14
|
public Window getWindow()
|
普通
|
取得Window对象
|
15
|
public void setOnShowListener(
DialogInterface.OnShowListener listener)
|
普通
|
设置对话框打开时监听
|
16
|
public void setOnDismissListener (
DialogInterface.OnDismissListener listener)
|
普通
|
对话框隐藏时监听
|
17
|
public void setOnCancelListener(
DialogInterface.OnCancelListener listener)
|
普通
|
设置对话框取消时监听
|
1
|
public AlertDialog.Builder(Context context)
|
构造
|
创建AlertDialog.Builder对象
|
2
|
public AlertDialog.Builder setMessage (int messageId)
|
普通
|
设置显示信息的资源ID
|
3
|
public AlertDialog.Builder setMessage (CharSequence message)
|
普通
|
设置显示信息的字符串
|
4
|
public AlertDialog.Builder setView(View view)
|
普通
|
设置显示的View组件
|
5
|
public AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener)
|
普通
|
设置对话框显示一个单选的List,指定默认选中项,同时设置监听处理操作
|
6
|
public AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface.OnClickListener listener)
|
普通
|
设置对话框显示一个单选的List,指定默认选中项,同时设置监听处理操作
|
7
|
public AlertDialog.Builder setMultiChoiceItems (CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
|
普通
|
设置对话框显示一个复选的List,同时设置监听处理操作
|
8
|
public AlertDialog.Builder setPositiveButton (CharSequence text, DialogInterface.OnClickListener listener)
|
普通
|
为对话框添加一个确认按钮,同时设置监听操作
|
9
|
public AlertDialog.Builder setPositiveButton (int textId, DialogInterface.OnClickListener listener)
|
普通
|
为对话框添加一个确认按钮,显示内容由资源文件指定,并设置监听操作
|
10
|
public AlertDialog.Builder setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
|
普通
|
为对话框设置一个取消按钮,并设置监听操作
|
11
|
public AlertDialog.Builder setNegativeButton (int textId, DialogInterface.OnClickListener listener)
|
普通
|
为对话框设置一个取消按钮,显示内容由资源文件指定,并设置监听操作
|
12
|
public AlertDialog.Builder setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener)
|
普通
|
设置一个普通按钮,并设置监听操作
|
13
|
public AlertDialog.Builder setNeutralButton(int textId, DialogInterface.OnClickListener listener)
|
普通
|
设置一个普通按钮,显示内容由资源文件指定,并设置监听操作
|
14
|
public AlertDialog.Builder setItems(CharSequence[] items, DialogInterface.OnClickListener listener)
|
普通
|
将信息内容设置为列表项,同时设置监听操作
|
15
|
public AlertDialog.Builder setItems(int itemsId, DialogInterface.OnClickListener listener)
|
普通
|
将信息内容设置为列表项,列表项内容由资源文件指定,同时设置监听操作
|
16
|
public AlertDialog create()
|
普通
|
创建AlertDialog的实例化对象
|
17
|
public AlertDialog.Builder setIcon(Drawable icon)
|
普通
|
设置显示的图标
|
18
|
public AlertDialog.Builder setIcon(int iconId)
|
普通
|
设置要显示图标的资源ID
|
<span style="font-size:18px;"><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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/button9" android:layout_alignTop="@+id/button1" android:layout_toRightOf="@+id/button3" android:text="自定义对话框" /> <Button android:id="@+id/button9" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button8" android:layout_alignParentBottom="true" android:text="时间选择对话框" /> <Button android:id="@+id/button8" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button9" android:layout_alignRight="@+id/button4" android:text="日期选择对话框" /> <Button android:id="@+id/button7" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button8" android:layout_alignLeft="@+id/button8" android:text="进度条对话框" /> <Button android:id="@+id/button6" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button7" android:layout_alignLeft="@+id/button7" android:text="带多选按钮对话框" /> <Button android:id="@+id/button5" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button6" android:layout_alignLeft="@+id/button6" android:text="带单选按钮对话框" /> <Button android:id="@+id/button4" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button5" android:layout_alignRight="@+id/button2" android:text="带item的对话框" /> <Button android:id="@+id/button3" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button4" android:layout_alignLeft="@+id/button4" android:text="带事件处理的对话框" /> <Button android:id="@+id/button2" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button3" android:layout_alignParentLeft="true" android:layout_marginLeft="21dp" android:text="带按钮的对话框" /> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignLeft="@+id/button3" android:text="最简单的Dialog" /> </RelativeLayout></span>
自定义custom.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/MyLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:text="用户名:" android:textSize="8pt" /> <EditText android:layout_height="wrap_content" android:width="60pt" /> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:text="密 码:" android:textSize="8pt" /> <EditText android:layout_height="wrap_content" android:password="true" android:width="60pt" /> ? </TableRow> </TableLayout></span>
JAVA文件
<span style="font-size:18px;">package com.example.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.DatePickerDialog.OnDateSetListener; import android.app.Dialog; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.app.TimePickerDialog.OnTimeSetListener; 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.DatePicker; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private Button button1, button2, button3, button4, button5, button6, button7, button8, button9,button10; private String CityData[] = { "河南", "北京", "上海", "天津", "湖南", "深圳" };//city数据 private String SexData[] = { "男", "女" };//性别数据 private String ColorData[] = { "红", "橙", "黄", "绿", "青", "蓝", "紫" };//颜色数据 private boolean ColorChoiced[] = { false, false, false, false, false, false, false };// 设置默认选中项 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 为按钮实例化 button1 = (Button) this.findViewById(R.id.button1); button2 = (Button) this.findViewById(R.id.button2); button3 = (Button) this.findViewById(R.id.button3); button4 = (Button) this.findViewById(R.id.button4); button5 = (Button) this.findViewById(R.id.button5); button6 = (Button) this.findViewById(R.id.button6); button7 = (Button) this.findViewById(R.id.button7); button8 = (Button) this.findViewById(R.id.button8); button9 = (Button) this.findViewById(R.id.button9); button10 = (Button) this.findViewById(R.id.button10); // 最简单的对话框 button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("关于我们")//设置标题 .setMessage("飞扬软件有限公司")//设置提示信息 .setIcon(R.drawable.a2)//设置图标 .create();//创建对话框 dialog.show();//显示对话框 } }); // 带按钮的信息处理对话框 button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("删除信息")//设置标题 .setMessage("您确定要删除该条信息吗?")//设置提示信息 .setIcon(R.drawable.a2)//设置图标 .setNegativeButton("取消", null)//设置取消按钮 .setPositiveButton("确定", null)//设置确定按钮 .create();//创建对话框 dialog.show();//显示对话框 } }); // 带事件处理的对话框 button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("退出程序") .setMessage("您确定要退出程序吗?") .setIcon(R.drawable.a2) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialogInterface, int whitch) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "程序即将退出", 2).show(); // finish(); 为了演示效果,不进行真实退出,用土司代替 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }).create(); dialog.show(); } }); // 对话框列表 button4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("请选择您的就业城市") .setIcon(R.drawable.a2) .setNegativeButton("取消", null) .setItems(CityData, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dInterface, int whitch) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您选择了:" + CityData[whitch], 2) .show(); } }).create(); dialog.show(); } }); // 带单选按钮的对话框 button5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("请选择您的性别") .setSingleChoiceItems(SexData, 0, new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dInterface, int whitch) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您选择了:" + SexData[whitch], 2) .show(); } }).setIcon(R.drawable.a2) .setNegativeButton("取消", null) .setPositiveButton("确定", null).create(); dialog.show(); } }); // 带多选框的对话框 button6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("请选择您喜欢的颜色") .setMultiChoiceItems( ColorData, ColorChoiced, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int whitch, boolean isChecked) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您选择了" + ColorData[whitch], 2) .show(); // 在这里没有做判断,所以无论是选中还是去选都会触发该事件,有兴趣的读者可以设置IsChecked判断 } }).setIcon(R.drawable.a2) .setNegativeButton("取消", null) .setPositiveButton("确定", null).create(); dialog.show(); } }); button7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub final ProgressDialog dialog = new ProgressDialog( MainActivity.this); dialog.setMessage("正在加载信息,请稍后!"); dialog.setTitle("正在加载"); dialog.setIcon(R.drawable.a2); dialog.onStart(); new Thread() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { dialog.dismiss(); } } }.start(); dialog.show(); } }); // 日期选择对话框 button8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub DatePickerDialog dialog = new DatePickerDialog( MainActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub Toast.makeText( MainActivity.this, "设置的时间为:" + year + "年" + (monthOfYear+1) //此处month要加1,原始月份从零开始了 + "月" + dayOfMonth + "日", 2) .show(); } }, 2014, 8, 17); dialog.show(); } }); // 时间选择对话框 button9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub TimePickerDialog dialog =new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker arg0, int hourOfDay, int minute) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "设置时间为:"+hourOfDay+"时"+minute+"分", 2).show(); } }, 20, 55, true); dialog.show(); } }); // 自定义对话框 button10.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO Auto-generated method stub View myView=LayoutInflater.from(getApplication()).inflate(R.layout.custom, null);//将layout对象转换为VIew对象 Dialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("用户登陆") .setIcon(R.drawable.a2) .setNegativeButton("取消", null) .setPositiveButton("登录", null) .setView(myView)//设置要显示的View组件 .create(); dialog.show(); } }); } } </span>
效果图
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。