2019 Jun. 30.
2019 Jun. 29.
2019 Jun. 22.
2019 May 31.
2019 May 30.
参照元
https://developer.android.com/guide/topics/ui/dialogs
https://akira-watson.com/android/alertdialog.html
http://furudate.hatenablog.com/entry/2014/01/09/162421
[MainActivity]
2019 Jun. 29.
2019 Jun. 22.
2019 May 31.
2019 May 30.
参照元
https://developer.android.com/guide/topics/ui/dialogs
https://akira-watson.com/android/alertdialog.html
http://furudate.hatenablog.com/entry/2014/01/09/162421
[MainActivity]
package YOUR.PACKAGE.alertdialogsample; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button button_dialog1, button_dialog2, button_dialog3; private TextView text_view; private FragmentManager flagmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text_view = findViewById(R.id.text_view); button_dialog1 = findViewById(R.id.button_dialog1); button_dialog2 = findViewById(R.id.button_dialog2); button_dialog3 = findViewById(R.id.button_dialog3); // button_dialog1ボタンタップでAlertを表示させる button_dialog1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { flagmentManager = getSupportFragmentManager(); // DialogFragment を継承したAlertDialogFragment1のインスタンス AlertDialogFragment1 dialogFragment = AlertDialogFragment1.newInstance(100); // DialogFragmentの表示 dialogFragment.show(flagmentManager, "test alert dialog1"); } }); // button_dialog2ボタンタップでダイアログのメッセージを変えたAlertを表示させる // newInstance()への引数を変更してメッセージを変える button_dialog2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { flagmentManager = getSupportFragmentManager(); // DialogFragmentを継承したAlertDialogFragment2のインスタンス AlertDialogFragment1 dialogFragment = AlertDialogFragment1.newInstance(200); // DialogFragmentの表示 dialogFragment.show(flagmentManager, "test alert dialog2"); } }); // button_dialog3ボタンタップでItemを選択するAlertを表示させる button_dialog3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { flagmentManager = getSupportFragmentManager(); // DialogFragment を継承したAlertDialogFragmentのインスタンス AlertDialogFragment2 dialogFragment = new AlertDialogFragment2(); // DialogFragmentの表示 dialogFragment.show(flagmentManager, "test alert dialog"); } }); } public void setTextView(String message){ text_view.setText(message); } /* * DialogFragmentを継承したクラスAlertDialogFragment1 */ public static class AlertDialogFragment1 extends DialogFragment { /* * コンストラクタを記述してはならない。 * newInstance()でのsetArguments、onCreateDialog()でのgetArgumentsを利用する。 */ public static AlertDialogFragment1 newInstance( int requestCode) { AlertDialogFragment1 fragment = new AlertDialogFragment1(); Bundle arguments = new Bundle(); arguments.putInt("reqCode", requestCode); fragment.setArguments(arguments); return fragment; } @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { int requestCode = getArguments().getInt("reqCode"); AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); switch (requestCode) { case 100: alert.setTitle("Test AlertDialog1"); alert.setMessage("Message is 100"); break; case 200: alert.setTitle("Test AlertDialog2"); alert.setMessage("Message is 200"); break; } alert.setPositiveButton(R.string.dialog_ok, null); return alert.create(); } } /* * DialogFragmentを継承したクラスAlertDialogFragment2 */ public static class AlertDialogFragment2 extends DialogFragment { // 選択肢のリスト private String[] menulist = {"選択(1)","選択(2)","選択(3)"}; @Override @NonNull public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); alert.setTitle("Test AlertDialog3"); alert.setItems(menulist, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int idx) { // 選択1 if (idx == 0) { setMassage(menulist[0]); } // 選択2 else if (idx == 1) { setMassage(menulist[1]); } // 選択3, idx == 2 else{ setMassage(menulist[2]); } } }); return alert.create(); } private void setMassage(String message) { MainActivity mainActivity = (MainActivity) getActivity(); if (mainActivity != null) { mainActivity.setTextView(message); } } } }
0 件のコメント:
コメントを投稿