Skip to content

Instantly share code, notes, and snippets.

@daipresents
Last active July 21, 2017 10:26
Show Gist options
  • Save daipresents/8ab63dc0173c36520ca54c7bbbec5855 to your computer and use it in GitHub Desktop.
Save daipresents/8ab63dc0173c36520ca54c7bbbec5855 to your computer and use it in GitHub Desktop.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
this.layout = inflater.inflate(R.layout.dialog, (ViewGroup) getActivity().findViewById(R.id.dialog_layout));
// TODO: ダイアログで、はい・いいえ の選択肢を表示する
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(layout);
builder.setMessage("Input your name.")
// OKボタン
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO: OKボタンを押した時は入力した情報を SharedPreferences に保存するように実装してください。
Log.v(AssignmentDialogFragment.class.getSimpleName(), "OK.");
EditText editText = (EditText) layout.findViewById(R.id.yourName);
String yourName = editText.getText().toString();
SharedPreferences sp = getActivity().getSharedPreferences("sample", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", yourName);
editor.commit();
Log.v(MainActivity.class.getSimpleName(), "name: " + yourName);
}
})
// Cancelボタン
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v(AssignmentDialogFragment.class.getSimpleName(), "Canceled.");
}
});
// Dialogを作成して返却
return builder.create();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// TODO: ダイアログで、はい・いいえ の選択肢を表示する
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.dialog);
builder.setMessage("Input your name.")
// OKボタン
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// TODO: OKボタンを押した時は入力した情報を SharedPreferences に保存するように実装してください。
Log.v(AssignmentDialogFragment.class.getSimpleName(), "OK.");
EditText editText = (EditText) getActivity().findViewById(R.id.yourName);
String yourName = editText.getText().toString();
SharedPreferences sp = getActivity().getSharedPreferences("sample", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("name", yourName);
editor.commit();
Log.v(MainActivity.class.getSimpleName(), "name: " + yourName);
}
})
// Cancelボタン
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.v(AssignmentDialogFragment.class.getSimpleName(), "Canceled.");
}
});
// Dialogを作成して返却
return builder.create();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment