简单的EditText Alert对话框在创建主活动之前运行

时间:2013-06-18 04:34:14

标签: java android sharedpreferences android-edittext android-alertdialog

尝试显示简单的编辑文本对话框,在应用程序的其余部分启动之前请求提供字符串。目前我试图让我的应用程序的APIKEY首先请求,然后一旦进入其保存共享首选项,然后对话框将不会显示。目前的代码正在从我的旧项目中重用。如果有人可以帮助我指出正确的对话框。

 public void getapikey() {

        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        LayoutInflater adbInflater = LayoutInflater.from(this);
        View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
        editText = (EditText) eulaLayout.findViewById(R.id.editText1);
        adb.setView(eulaLayout);
        adb.setTitle("Api Key");
        adb.setMessage("Welcome to the app, Please input your APIkey below");
        adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            // CheckBox Confirm for Alert Dialog
            public void onClick(DialogInterface dialog, int which) {
                String value = editText.getText().toString();
                                    if (editText !=null)
                                    //Unsure about this part above and below
                    editText = "0"
                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("apikey", value);
                // Commit the edits!
                editor.commit();
                return;
            }
        });

        // Preferences For Alert Dialog
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String apikey = settings.getString("apikey", "0");
        if (apikey!=null )
            adb.setIcon(R.drawable.ic_launcher);
            adb.show();

        }
    }

推荐的更改

公共类欢迎扩展活动{

public static final String PREFS_NAME = "MyPrefsFile";
public EditText editText;
public String value;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getapikey();
}

public void getapikey() {

    // Alert Dialog
    AlertDialog.Builder adb = new AlertDialog.Builder(this);
    LayoutInflater adbInflater = LayoutInflater.from(this);
    View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
    // dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.checkBox1);
    editText = (EditText) eulaLayout.findViewById(R.id.editText1);
    adb.setView(eulaLayout);
    adb.setTitle("Api Key");
    adb.setMessage("Welcome to the app, Please input your APIkey below");
    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            //String checkBoxResult = "NOT checked";
            String value = editText.getText().toString();
            // if (dontShowAgain.isChecked())
            // checkBoxResult = "checked";
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            //editor.putString("skipMessage", checkBoxResult);
            editor.putString("apikey", value);
            // Commit the edits!
            editor.commit();
            return;
        }
    });

    // Preferences For Alert Dialog
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    //String skipMessage = settings.getString("skipMessage", "NOT checked");
    String apikey = settings.getString("apikey", value);
    if(!value.equals(""))
                    adb.setIcon(R.drawable.ic_launcher);
        adb.show();

        setContentView(R.layout.splash_screen);
        Thread splashThread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    // changed from 5000 to 4000 11.29
                    while (waited < 3000) {
                        sleep(100);
                        waited += 100;
                    }
                } catch (InterruptedException e) {
                    // do nothing
                } finally {
                    Intent i = new Intent();
                    i.setClassName("com.example.app",
                            "com.example.app.CardsTesting");
                    startActivity(i);
                    finish();
                }
            }
        };
        splashThread.start();
    }
}

在第一次之后仍然没有保存首选项,然后永远不会显示

1 个答案:

答案 0 :(得分:0)

//尝试这个我觉得它可能有效

SharedPreferences settings = PreferenceManager.getSharedPreferences(PREFS_NAME,0);