2

尝试显示简单的编辑文本对话框,请求在我的应用程序的其余部分启动之前提供一个字符串。目前我试图做到这一点,所以我的应用程序的 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();
    }
}

第一次后仍然不保存首选项然后永远不会显示

4

1 回答 1

0

//试试这个,我认为它可能有效

SharedPreferences 设置 = PreferenceManager.getSharedPreferences(PREFS_NAME, 0);

于 2013-06-18T06:44:51.537 回答