Java 如何让alertDialog在每次运行应用程序时弹出?

Java 如何让alertDialog在每次运行应用程序时弹出?,java,android,android-alertdialog,android-preferences,Java,Android,Android Alertdialog,Android Preferences,安装应用程序后,将弹出一个警报对话框。我希望它有一个编辑文本字段,询问用户的姓名。回答之后,它就再也不会出现了。然后,每次我打开应用程序时,另一个alertDialog会像向用户打招呼一样弹出 公共类MainActivity扩展活动{ 最终上下文=此; @凌驾 创建时受保护的void(Bundle savedInstanceState){ super.onCreate(savedInstanceState); AlertDialog.Builder alert=新建AlertDialog.Bui

安装应用程序后,将弹出一个
警报对话框
。我希望它有一个编辑文本字段,询问用户的姓名。回答之后,它就再也不会出现了。然后,每次我打开应用程序时,另一个
alertDialog
会像向用户打招呼一样弹出

公共类MainActivity扩展活动{
最终上下文=此;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
AlertDialog.Builder alert=新建AlertDialog.Builder(上下文);
警报。设置标题(“欢迎”);
alert.setMessage(“在此处输入您的姓名”);
最终编辑文本输入=新编辑文本(上下文);
alert.setView(输入);
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
} 
});
AlertDialog AlertDialog=alert.create();
alertDialog.show();
AlertDialog.Builder a\u Builder=新建AlertDialog.Builder(MainActivity.this);
a_builder.setMessage(“Mabuhay!”)
.setCancelable(真)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
AlertDialog alert=a_builder.create();
alert.show();
}
}

用户在首次打开应用程序时输入其姓名后,您可以将给定用户名写入应用程序的

当用户现在在任何其他时间打开应用程序时,您只需检查应用程序的SharedReferences中是否有条目,然后用适当的用户名问候用户

//instance variable in the Mainactivity.class
private boolean showMessage = true;

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String username = sharedPref.getString("username", null);
if(username == null){
    //first time user - ask for username
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("username", enteredName);
    editor.commit();
    showMessage = false;
} else if(showMessage) {
    showMessage = false;
    //greet
    AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
    alert.setMessage("Hello " + username + "!")
            .setCancelable(true)
            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    alert.create().show();
}

您可以使用两种方法来执行此操作。 第一个是通过像您的示例一样使用警报对话框,您可以在这里找到如何使用对话框

或者,您可以通过在清单中设置对话框主题来创建活动对话框,您可以找到它


如需进一步解释,请让我知道

天哪,这真的管用D非常感谢!你是我的救世主!我在这件事上浪费了好几个小时。还有一个问题,我如何将刚刚输入的字符串用户名传输到问候语部分?比如“你好!在这里插入姓名”,而且,每次我返回主活动时,问候语都会再次弹出。我怎样才能控制问候语只能在我第一次运行应用程序的时候进行?关于第一个问题,请参阅编辑的帖子。要检查问候语消息是否仅显示在应用程序的开头,只需向MainActivity添加一个实例变量布尔标志。如果标志为true,则简单打印消息,并在打印后将其设置为false。现在也在答案中编辑。如果用户刚刚输入用户名,是否也要打印hello消息?因为需要在代码中做一些小的更改,我想用户输入姓名的时间有一个例外,然后它将在第二次之后显示。。如果我让这件事对你来说有点太难了,我很抱歉。:)我编辑了这篇文章来整理一下。您应该避免将代码段用于非基于web的代码(HTML/CSS/Javascript),因为它只有在您可以在浏览器中运行代码时才有用。对于Java,只需粘贴代码,选择它,然后使用编辑器中的
{}
按钮将其设置为代码块。有关在堆栈溢出时格式化帖子的详细信息,请参阅。
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);
AlertDialog dialog = builder.create();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
           }
       });
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancelled the dialog
           }
       });
// Set other dialog properties
...

// Create the AlertDialog
AlertDialog dialog = builder.create();