Android 简单警报对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26097513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android simple alert dialog
提问by LS_
I need to show a little text message to the users that clicks a button on my Android app, on IOS I just had to create an AlertView that it's simple to use but with Android i'm struggling because the solution seems x10 times harder. I saw that I need to use a DialogFragment but I can't understand how to make it work, can someone explain? Also, is my solution right or there is something easier to show a simple text message to users?
我需要向点击我的 Android 应用程序上的按钮的用户显示一条小短信,在 IOS 上,我只需要创建一个易于使用的 AlertView,但对于 Android,我正在努力,因为该解决方案似乎要困难 10 倍。我看到我需要使用 DialogFragment 但我不明白如何使它工作,有人可以解释一下吗?另外,我的解决方案是否正确,或者有什么更容易向用户显示简单短信的方法?
回答by MysticMagic?
You would simply need to do this in your onClick
:
你只需要在你的onClick
:
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
I don't know from where you saw that you need DialogFragment for simply showing an alert.
我不知道你从哪里看到你需要 DialogFragment 来简单地显示警报。
Hope this helps.
希望这可以帮助。
回答by Sagar Pilkhwal
No my friend its very simple, try using this:
不,我的朋友很简单,尝试使用这个:
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Welcome to dear user.");
alertDialog.setIcon(R.drawable.welcome);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
This tutorialshows how you can create custom dialog using xml and then show them as an alert dialog.
本教程展示了如何使用 xml 创建自定义对话框,然后将它们显示为警报对话框。
回答by greenapps
You can easily make your own 'AlertView' and use it everywhere.
您可以轻松制作自己的“AlertView”并随处使用。
alertView("You really want this?");
Implement it once:
实现一次:
private void alertView( String message ) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle( "Hello" )
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
// .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialoginterface, int i) {
// dialoginterface.cancel();
// }})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
}
}).show();
}