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

时间:2016-01-01 23:29:03

标签: java android alertdialog android-alertdialog android-preferences

安装应用后,会弹出alertDialog。我希望它有一个编辑文本字段,询问用户的名称。回答后,它永远不会再显示出来。然后,每次打开应用程序时,另一个alertDialog将弹出,就像问候用户一样。

public class MainActivity extends Activity {
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AlertDialog.Builder alert = new AlertDialog.Builder(context);
        alert.setTitle("Welcome");
        alert.setMessage("Enter Your Name Here"); 
        final EditText input = new EditText(context);
        alert.setView(input);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            } 
        });

        AlertDialog alertDialog = alert.create();
        alertDialog.show();

        AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
        a_builder.setMessage("Mabuhay!")
                .setCancelable(true)
                .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = a_builder.create();
        alert.show();
    }
}

2 个答案:

答案 0 :(得分:1)

用户在第一次打开应用程序时输入了他的名字后,您可以将给定的用户名写入应用程序的SharedPreferences

当用户现在在任何其他时间打开应用程序时,您只需检查您的应用程序的共享首选项中是否有条目,并使用适当的用户名向用户致意。

//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();
}

答案 1 :(得分:1)

您可以使用两种方法执行此操作。 第一种是使用类似示例的警告对话框,您可以在此处找到如何使用对话框。

Dialogs by Android Developer

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();

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

进一步说明让我知道!