Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

android - 如何在Android上显示警报对话框?(How do I display an alert dialog on Android?)

I want to display a dialog/popup window with a message to the user that shows "Are you sure you want to delete this entry?"

(我想显示一个对话框/弹出窗口,并向用户显示“您确定要删除此条目吗?”的消息。)

with one button that says 'Delete'.

(一个带有“删除”按钮。)

When Delete is touched, it should delete that entry, otherwise nothing.

(触摸Delete ,它应删除该条目,否则不删除任何条目。)

I have written a click listener for those buttons, but how do I invoke a dialog or popup and its functionality?

(我已经为这些按钮编写了一个单击侦听器,但是如何调用对话框或弹出窗口及其功能?)

  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use an AlertDialog for this and construct one using its Builder class.

(您可以为此使用AlertDialog并使用其Builder类构造一个AlertDialog 。)

The example below uses the default constructor that only takes in a Context since the dialog will inherit the proper theme from the Context you pass in, but there's also a constructor that allows you to specify a specific theme resource as the second parameter if you desire to do so.

(下面的示例使用默认构造函数,该构造函数仅接受Context因为对话框将从您传入的Context中继承适当的主题,但是如果需要,还有一个构造函数可让您将特定的主题资源指定为第二个参数。这样做。)

new AlertDialog.Builder(context)
    .setTitle("Delete entry")
    .setMessage("Are you sure you want to delete this entry?")

    // Specifying a listener allows you to take an action before dismissing the dialog.
    // The dialog is automatically dismissed when a dialog button is clicked.
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // Continue with delete operation
        }
     })

    // A null listener allows the button to dismiss the dialog and take no further action.
    .setNegativeButton(android.R.string.no, null)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...