アプリケーションの残りの部分が開始する前に、文字列を提供するように要求して、単純なテキスト編集ダイアログを表示しようとしています。現在、アプリの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 class Welcome extends Activity {
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();
}
}
初めて設定を保存せず、表示されない