Android 应用程序启动时提示输入用户名,并在用户下次启动应用程序时问候用户

Android 应用程序启动时提示输入用户名,并在用户下次启动应用程序时问候用户,android,Android,我正在构建一个android应用程序,它要求我在用户第一次启动应用程序时提示用户输入他的名字。在第二次之后,应用程序将不会提示用户输入他的姓名,而是向用户打招呼(并直接转到主菜单页。有人知道我可以怎么做吗?使用以下代码: File file = new File("username.txt"); // if file doesnt exists, then prompt username alert dialog if (!file.exists

我正在构建一个android应用程序,它要求我在用户第一次启动应用程序时提示用户输入他的名字。在第二次之后,应用程序将不会提示用户输入他的姓名,而是向用户打招呼(并直接转到主菜单页。有人知道我可以怎么做吗?

使用以下代码:

        File file = new File("username.txt");

        // if file doesnt exists, then prompt username alert dialog
        if (!file.exists()) {
          //prompt for username
          file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        //suppose  EditText_UserName is your edittext where he can enter his name
        bw.write(EditText_UserName.getText().toString());
        bw.close();
每次启动应用程序时,请使用file.exists检查此文件并继续。

使用以下代码:

        File file = new File("username.txt");

        // if file doesnt exists, then prompt username alert dialog
        if (!file.exists()) {
          //prompt for username
          file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        //suppose  EditText_UserName is your edittext where he can enter his name
        bw.write(EditText_UserName.getText().toString());
        bw.close();

每次启动应用程序时,请使用file.exists检查此文件并继续。

将用户名存储在首选项字段中。当应用程序启动时,请检查并查看此首选项字段是否已设置,如果未设置,则提示用户输入用户名并保存,如果已设置,则检索该用户名并在您的活动中使用:

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String userName = prefs.getString("user_name", null);
    if (userName == null) {
        EditText input = new EditText(this);
        input.setId(1000);          
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(input).setTitle("Enter your username!")
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                EditText theInput = (EditText) ((AlertDialog) dialog)
                                        .findViewById(1000);
                                String enteredText = theInput.getText()
                                        .toString();
                                if (!enteredText.equals("")) {
                                    SharedPreferences.Editor editor = prefs
                                            .edit();
                                    editor.putString("user_name",
                                            enteredText);
                                    editor.commit();
                                }
                            }
                        }).create();
        dialog.show();
    } 
    // you are ready to use the user's username

将用户名存储在首选项字段中。当应用程序启动时,检查并查看此首选项字段是否已设置,如果未设置,则提示用户输入用户名并保存,如果已设置,则检索该用户名并在您的活动中使用:

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    String userName = prefs.getString("user_name", null);
    if (userName == null) {
        EditText input = new EditText(this);
        input.setId(1000);          
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setView(input).setTitle("Enter your username!")
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                EditText theInput = (EditText) ((AlertDialog) dialog)
                                        .findViewById(1000);
                                String enteredText = theInput.getText()
                                        .toString();
                                if (!enteredText.equals("")) {
                                    SharedPreferences.Editor editor = prefs
                                            .edit();
                                    editor.putString("user_name",
                                            enteredText);
                                    editor.commit();
                                }
                            }
                        }).create();
        dialog.show();
    } 
    // you are ready to use the user's username

那么我如何将此活动导航到另一个页面?我必须使用新的意图吗?@honeybee您不必从此活动导航。在您拥有的每个活动中(或用户在启动应用程序时将看到的第一个活动)您将放置此代码。如果用户没有用户名,则将弹出对话框并保存用户名,否则用户已存储用户名,您将使用它来问候他。在其他活动中,您只需从首选项中获取字符串并使用它。在用户键入其名称后,我如何指向其他页面?例如,主菜单页面?很抱歉问了太多问题,因为我对android编程非常陌生。@honeybee如果你想将用户引导到另一个页面,那么你将使用
意图
。如果我不想使用diaglog框,只想使用文本视图“输入用户名”的普通编辑文本,然后将名称显示到下一个活动,我该如何操作?我知道读取用户输入、指向其他活动的代码,但我不知道如何使用shared pref存储用户名并在下一个活动中显示它…那么我如何将此活动导航到另一个页面?我必须使用新的意图吗?@honeybee您不必导航e。在您的每个活动中(或用户启动应用程序时将看到的第一个活动)您将放置此代码。如果用户没有用户名,则将弹出对话框并保存用户名,否则用户已存储用户名,您将使用它来问候他。在其他活动中,您只需从首选项中获取字符串并使用它。在用户键入其名称后,我如何指向其他页面?例如,主菜单页面?很抱歉问了太多问题,因为我对android编程非常陌生。@honeybee如果你想将用户引导到另一个页面,那么你将使用
意图
。如果我不想使用diaglog框,只想使用文本视图“输入用户名”的普通编辑文本,然后在下一个活动中显示用户名,我该怎么做?我知道读取用户输入、指向其他活动的代码,但我不知道如何使用shared pref存储用户名并在下一个活动中显示它…这是否意味着不同的用户可以使用该应用程序?如何将其定向到另一个页面?例如,菜单页面?是的。这是一个问题E将考虑所有的用户。如果你想切换用户,你应该有一个注销类型的选项,其中你将改变文件的内容。但是如果你想确保不同的用户输入他们自己的名字,那么你应该修改你的要求……它应该像应用程序建议一个名字每次。是打开的(不是直接从文件中读取)。直接到另一个页面是什么意思?如果你是指一个新的活动,你可以查看(此处)这是否意味着不同的用户可以使用这个应用程序?你是如何把它引导到另一个页面的?比如说菜单页?是的。这个代码会考虑所有的用户。如果你想切换用户,你应该有一个注销类型的选项,其中你将改变文件的内容。但是如果你想确保不同的用户输入他们自己的N。ame,那么您应该修改您的要求…它应该像应用程序每次打开时建议的名称一样(而不是直接从文件中读取)。直接转到另一个页面是什么意思?如果您指的是新活动,您可以查看(此处)[