Android简单警报对话框
我需要向用户显示一条短信,在我的Android应用程序上点击一个按钮,在IOS上我只需要创建一个AlertView,它使用起来很简单,但在Android上我很挣扎,因为这个解决方案似乎要困难10倍。我看到我需要使用DialogFragment,但我不明白如何让它工作,有人能解释一下吗?另外,我的解决方案是否正确,或者是否有更容易向用户显示简单文本消息的方法?您只需在Android简单警报对话框,android,dialog,android-alertdialog,Android,Dialog,Android Alertdialog,我需要向用户显示一条短信,在我的Android应用程序上点击一个按钮,在IOS上我只需要创建一个AlertView,它使用起来很简单,但在Android上我很挣扎,因为这个解决方案似乎要困难10倍。我看到我需要使用DialogFragment,但我不明白如何让它工作,有人能解释一下吗?另外,我的解决方案是否正确,或者是否有更容易向用户显示简单文本消息的方法?您只需在onClick中执行此操作: AlertDialog alertDialog = new AlertDialog.Builder(M
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();
我不知道您从哪里看到,您需要DialogFragment来简单地显示警报
希望这有帮助。不,我的朋友,这很简单,请尝试使用以下方法:
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();
这显示了如何使用xml创建自定义对话框,然后将它们显示为警报对话框。您可以轻松创建自己的“警报视图”,并在任何地方使用它
alertView("You really want this?");
实施一次:
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();
}
您还没有通过which button.FYI-谷歌Android开发网站上的第一个示例展示了如何使用片段来实现这一点:我认为这可能会导致开发人员认为他需要使用片段来创建基本的AlertDialog。我今天搜索了一下,觉得可能是这样。最好在生成器上设置属性,而不是alertDialog实例!