INTRODUCING DIALOGS

本文介绍了在Android中实现对话框的三种方法:使用Dialog类及其扩展、应用对话框主题的活动以及使用Toast显示短暂消息。此外,还提供了创建AlertDialog的具体步骤,并展示了如何管理和显示对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There are three ways to implement a dialog in Android: .

 

1. Using the Dialog class (or its extensions) As well
as the general-purpose AlertDialog class, Android
includes a number of specialist classes that extend
Dialog.

 

2.Dialog-themed Activities You can apply the
dialog theme to a regular Activity to give it the
appearance of a standard dialog box.

 

3.Toasts Toasts are special non-modal transient message boxes, often used by Broadcast
Receivers and Services to notify users of events occurring in the background.

 

Creating a new dialog using the Dialog class

Dialog d = new Dialog(MyActivity.this);
// Have the new window tint and blur the window it
// obscures.
Window window = d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
// Set the title
d.setTitle("Dialog Title");
// Inflate the layout
d.setContentView(R.layout.dialog_view);
// Find the TextView used in the layout
// and set its text value
TextView text = (TextView)d.findViewById(R.id.dialogTextView);
text.setText("This is the text in my dialog");
Once it’s configured to your liking, use the show method to display it.
d.show();

 

 

The Alert Dialog Class

AlertDialog.Builder ad = new AlertDialog.Builder(context);

 

Context context = MyActivity.this;
String title = "It is Pitch Black";
String message = "You are likely to be eaten by a grue.";
String button1String = "Go Back";
String button2String = "Move Forward";
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle(title);
ad.setMessage(message);
ad.setPositiveButton(button1String,new OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
eatenByGrue();
}
});
ad.setNegativeButton(button2String,
new OnClickListener(){
public void onClick(DialogInterface dialog, int arg1) {
// do nothing
}
});
ad.setCancelable(true);
ad.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
eatenByGrue();
}
});
To display an Alert Dialog that you’ve created call show:
ad.show();

 

Managing and Displaying Dialogs

static final private int TIME_DIALOG = 1;
@Override
public Dialog onCreateDialog(int id) {
switch(id) {
case (TIME_DIALOG) :
AlertDialog.Builder timeDialog = new AlertDialog.Builder(this)
timeDialog.setTitle("The Current Time Is...");
timeDialog.setMessage("Now");
return timeDialog.create();
}
return null;
}

 

@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch(id) {
case (TIME_DIALOG) :
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

Date currentTime = new Date(java.lang.System.currentTimeMillis());
String dateString = sdf.format(currentTime);
AlertDialog timeDialog = (AlertDialog)dialog;
timeDialog.setMessage(dateString);
break;
}
}

 

外部调用 showDialog(TIME_DIALOG);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值