所以我使用以下代码的目的是简单地启动此活动一次,获取APIKEY,存储它,然后启动使用该APIKEY的主要活动。此活动只是一个EditText对话框,向用户提供APIKEY,一旦输入,单击确定按钮,我想检查APIKEY并确保它不为空,然后再也不显示此活动,只需启动主未来的活动。
当前问题:活动仍在启动,但它只是简单的null,没有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;
}
}
答案 0 :(得分:1)
不要使用getIntent()
,因为它表示启动您当前所在活动的意图。而是尝试实例化您的意图(在launchActivity()
和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;
}