0

因此,我使用以下代码的目标是简单地启动此活动 1 次,获取 APIKEY,存储它,然后启动使用该 APIKEY 的主要活动。此活动只是一个 EditText 对话框,提示用户输入 APIKEY,一旦输入并单击 OK 按钮,我想检查 APIKEY 并确保其不为空,然后不再显示此活动,只需启动主活动进行中。

当前问题:Activity 仍在启动,但它只是简单地为空,没有 EditText 弹出窗口,并且永远不会启动 Intent 到主要活动。有没有更好的方法来做到这一点,然后我在下面怎么做!

代码

public class Welcome extends Activity {

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    apikey = settings.getString("apikey", "");
    getapikey();
            launchActivity();
    setContentView(R.layout.splash_screen);
}

private void launchActivity() {
    // TODO Auto-generated method stub
    Intent intent = getIntent();
    intent.setClassName("com.example.test",
            "com.example.test.CardsTesting");
    startActivity(intent);
}

public void getapikey() {
    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 Needed!");
    adb.setMessage("In Order to use this application, You will need a API Key from Enphase Energy. Please input your key below to get started :)");
    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();

            Intent intent = getIntent();
            intent.setClassName("com.example.test",
                    "com.example.test.CardsTesting");
            startActivity(intent);
            return;
        }
    });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");
    if (!skipMessage.equals("checked")) {
        // if (skipMessage !=("checked") )
        adb.setIcon(R.drawable.ic_launcher);
        adb.show();
        return;

    }
}
4

1 回答 1

1

不要使用getIntent()它表示启动您当前所在的 Activity 的意图。相反,尝试像这样实例化您的意图(inlaunchActivity()onClick()方法):

 Intent intent = new Intent(Welcome.this, com.example.test.CardsTesting.class);
 startActivity(intent);

我在你的课堂上做了一些改变,看看是否有帮助。

public class Welcome extends Activity {

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    apikey = settings.getString("apikey", "");
    if (getapikey()) {
        launchActivity();
        finish(); // requests the current activity (the splash screen) to be closed
    }
    setContentView(R.layout.splash_screen);
}

private void launchActivity() {
    Intent intent = new Intent(Welcome.this, com.example.test.CardsTesting.class);
    startActivity(intent);
}

public boolean getapikey() {
    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 Needed!");
    adb.setMessage("In Order to use this application, You will need a API Key from Enphase Energy. Please input your key below to get started :)");
    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();

        Intent intent = new Intent(Welcome.this, com.example.test.CardsTesting.class);
        startActivity(intent);
        Welcome.this.finish(); // requests the current activity (the splash screen) to be closed
        return;
    }
    });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    String skipMessage = settings.getString("skipMessage", "NOT checked");
    if (!skipMessage.equals("checked")) {
        // if (skipMessage !=("checked") )
        adb.setIcon(R.drawable.ic_launcher);
        adb.show();
        return true;
    }

    return false;
}
于 2013-06-27T23:23:36.083 回答