質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

2回答

2737閲覧

FragmentからDialogFragmentに値を渡したい

kbayashi

総合スコア18

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2020/04/06 06:26

編集2020/04/06 06:41

今現在todoアプリを作っております。
ダイアログのImageView上にあるボタンをどちらか押すと、黒い線で囲まれている部分のデザインと処理を切り替えたいと思い、その部分をフラグメントにしましたが、分から無いことがあります。

##問題
dialog上のフラグメント内にある真中のボタンを押すと、MaterialDateTimePickerのDatePickerDialogとTimePickerDialogが表示され、日付と時刻を選択した後に保存ボタンをタップすると、その時刻に通知するという処理を今作っているのですが、その処理の途中ででフラグメントからダイアログフラグメントに選択した日付を渡す必要があるのですが、どう渡せば良いのでしょうか?

!イメージ説明

ReminderDialog

kotlin

1class ReminderDialog : DialogFragment() 2{ 3 4 interface Listener{ 5 fun reminderUp(date:Date, identifier:String) 6 } 7 private var listener: Listener? = null 8 private var dialogDateTime: String? = null 9 private var dateTime:Date? = null 10 11 12 //フラグメントがアクティビティから呼び出されたとき 13 override fun onAttach(context: Context) { 14 super.onAttach(context) 15 when(context){ 16 is Listener -> listener = context 17 } 18 } 19 20 21 22 23 override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { 24 25 //ダイアログのインスタンス取得 26 val dialog:Dialog = Dialog(activity!!) 27 //タイトルバーなしのダイアログを表示 28 dialog.window?.requestFeature(Window.FEATURE_NO_TITLE) 29 //フルスクリーンでダイアログを表示 30 dialog.window?.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 31 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN) 32 //レイアウト指定 33 dialog.setContentView(R.layout.reminder_layout) 34 val now = Calendar.getInstance() 35 36 //時刻が夜の6時より前だったら太陽ボタンを消す 37 /*if(now.get(Calendar.HOUR) <= 18 && now.get(Calendar.HOUR) >= 6){ 38 dialog.reminderMorningButton.visibility = View.GONE 39 }else{ 40 dialog.reminderNightButton.visibility = View.GONE 41 }*/ 42 43 //背景色を透明に 44 dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) 45 dialog.window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 46 47 48 //保存ボタンを押したら通知を指定時間にセット 49 dialog.saveButton.setOnClickListener { 50 when{ 51 dateTime != null ->{ 52 val setDateTime = dateTime 53 if(setDateTime !=null) 54 listener?.reminderUp(setDateTime,"custum") 55 } 56 } 57 } 58 59 dialog.cancelButton.setOnClickListener { 60 dialog.cancel() 61 } 62 63 64 return dialog 65 } 66 67 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 68 super.onViewCreated(view, savedInstanceState) 69 70 /*val fragment = ReminderSelectFragment() 71 childFragmentManager.beginTransaction().add(R.id.reminderSelectFragment,fragment).commit()*/ 72 } 73 74 //文字をDateに変換 75 private fun String.toDate(pattern: String = "yyyy/MM/dd HH:mm"): Date?{ 76 return try { 77 SimpleDateFormat(pattern).parse(this) 78 }catch (e: IllegalArgumentException){ 79 return null 80 }catch (e: ParseException){ 81 return null 82 } 83 } 84 85 override fun onDetach() { 86 super.onDetach() 87 listener = null 88 } 89 90 //ダイアログを閉じる際フラグメントを削除 91 override fun onDestroy() { 92 super.onDestroy() 93 val fragment = reminderSelectFragment 94 val fragmentManager = activity!!.supportFragmentManager 95 fragmentManager.beginTransaction().remove(fragment).commit() 96 } 97}

ReminderSelectFragment

kotlin

1/** 2 * A simple [Fragment] subclass. 3 */ 4class ReminderSelectFragment : Fragment() , TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener{ 5 6 private var dialogDateTime: String? = null 7 private var dateTime:Date? = null 8 9 //時刻を選択したら文字列型の日付をDialogに渡す 10 override fun onTimeSet(view: TimePickerDialog?, hourOfDay: Int, minute: Int, second: Int){ 11 val time = "%1$02d:%2$02d".format(hourOfDay, minute) 12 dialogDateTime += " ${time}" 13 } 14 //日付が選択された 15 override fun onDateSet(view: DatePickerDialog?, year: Int, monthOfYear: Int, dayOfMonth: Int) { 16 val c = Calendar.getInstance() 17 c.set(year, monthOfYear, dayOfMonth) 18 val date = DateFormat.format("yyyy/MM/dd", c).toString() 19 dialogDateTime = date 20 showTimePickerDialog(date) 21 } 22 23 //時刻選択ダイアログ 24 fun showTimePickerDialog(date:String) { 25 val now = Calendar.getInstance() 26 val dpd = TimePickerDialog.newInstance( 27 this, 28 now.get(Calendar.HOUR), 29 now.get(Calendar.MINUTE)+1, 30 now.get(Calendar.SECOND), 31 false 32 ) 33 var test = now.get(Calendar.YEAR).toString() + "/0" + ((now.get(Calendar.MONTH)+1).toString()) + "/" + now.get(Calendar.DAY_OF_MONTH).toString() 34 Log.d("HOUR","${now.get(Calendar.HOUR)}") 35 //指定された日付が現在日付ならば現在時刻の五分以降より前の時刻は選択させない 36 if(date == test) { 37 dpd.setMinTime(Timepoint(now.get(Calendar.HOUR),(now.get(Calendar.MINUTE)+1))) 38 } 39 40 dpd.show(fragmentManager!!, "aaaa") 41 } 42 43 override fun onCreateView( 44 inflater: LayoutInflater, container: ViewGroup?, 45 savedInstanceState: Bundle? 46 ): View? { 47 // Inflate the layout for this fragment 48 var view = inflater.inflate(R.layout.fragment_reminder_select, container, false) 49 50 //日付選択ダイアログ呼び出し 51 view.reminderCustomButton.setOnClickListener { 52 val now = Calendar.getInstance() 53 val dpd = com.wdullaer.materialdatetimepicker.date.DatePickerDialog.newInstance( 54 this, 55 now.get(Calendar.YEAR), // Initial year selection 56 now.get(Calendar.MONTH), // Initial month selection 57 now.get(Calendar.DAY_OF_MONTH) // Inital day selection 58 ) 59 dpd.minDate = now 60 dpd.show(fragmentManager!!, "Datepickerdialog") 61 } 62 return view 63 } 64 65}

reminder_layout.xml

Xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:id="@+id/linearLayout" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content" 8 android:layout_margin="16dp" 9 android:background="@drawable/shape_rounded_corners_5dp"> 10 11 <fragment 12 android:id="@+id/reminderSelectFragment" 13 android:name="com.example.mytask.ReminderSelectFragment" 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_marginTop="24dp" 17 app:layout_constraintEnd_toEndOf="parent" 18 app:layout_constraintStart_toStartOf="parent" 19 app:layout_constraintTop_toBottomOf="@+id/radioGroup2" 20 tools:layout="@layout/fragment_reminder_select" /> 21 22 <RadioGroup 23 android:id="@+id/radioGroup2" 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_marginStart="24dp" 27 android:layout_marginTop="24dp" 28 android:layout_marginEnd="24dp" 29 android:orientation="horizontal" 30 app:layout_constraintEnd_toEndOf="parent" 31 app:layout_constraintStart_toStartOf="parent" 32 app:layout_constraintTop_toBottomOf="@+id/textView"> 33 34 <RadioButton 35 android:id="@+id/reminderOneTimeRadio" 36 android:layout_width="156dp" 37 android:layout_height="wrap_content" 38 android:background="@drawable/image_view_maru" 39 android:text="1回" 40 android:textAlignment="center" 41 android:textSize="20sp" /> 42 43 <RadioButton 44 android:id="@+id/reminderRepeatRadio" 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:background="@drawable/image_view_maru" 48 android:text="リピート" 49 android:textAlignment="center" 50 android:textSize="20sp" /> 51 </RadioGroup> 52 53 <TextView 54 android:id="@+id/textView" 55 android:layout_width="0dp" 56 android:layout_height="wrap_content" 57 android:background="@drawable/text_view_rounded" 58 android:text="リマインダー追加" 59 android:textColor="#000000" 60 android:textSize="35sp" 61 android:textStyle="bold" 62 app:layout_constraintEnd_toEndOf="parent" 63 app:layout_constraintStart_toStartOf="parent" 64 app:layout_constraintTop_toTopOf="parent" /> 65 66 <LinearLayout 67 android:id="@+id/linearLayout2" 68 android:layout_width="match_parent" 69 android:layout_height="0dp" 70 android:layout_marginTop="24dp" 71 android:orientation="horizontal" 72 app:layout_constraintEnd_toEndOf="parent" 73 app:layout_constraintStart_toStartOf="parent" 74 app:layout_constraintTop_toBottomOf="@+id/reminderSelectFragment"> 75 76 <Button 77 android:id="@+id/cancelButton" 78 android:layout_width="match_parent" 79 android:layout_height="match_parent" 80 android:layout_weight="1" 81 android:background="@drawable/botton_border" 82 android:text="キャンセル" 83 android:textSize="25sp" 84 app:layout_constraintStart_toStartOf="parent" /> 85 86 <Button 87 android:id="@+id/saveButton" 88 android:layout_width="match_parent" 89 android:layout_height="match_parent" 90 android:layout_weight="1" 91 android:background="@drawable/botton_border" 92 android:text="保存" 93 android:textSize="25sp" /> 94 </LinearLayout> 95 96</androidx.constraintlayout.widget.ConstraintLayout> 97

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kbayashi

2020/04/07 01:06

回答ありがとうございます。 また質問なんですけど、getParentFragmentを使うには、ReminderDialogのOnCreateDialog時にchildFragmentManager.beginTransaction.addで、ReminderSelectFragmentを追加する必要がありますか?
guest

回答2

0

自己解決

フラグメントからフラグメントを作成したり、フラグメント同士でデータのやりとりをするのは、
原因のわからない(正しいはずと思えてしまう)動作の不安定さを招きますのでなるべく避けることをお勧めします。と先生にゆわれたのでダイアログフラグメントの中にフラグメントを表示するのは辞めて、ラジオボタンを切り替えるごとで、Viewを表示・非表示させデザインと処理を変更しようと思います。

親身なアドバイスを無下にしてしまいすいません。

投稿2020/04/11 10:08

kbayashi

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

jimbe

2020/04/12 06:14

見た所からして(再利用等を考慮する) Fragment よりは, JPanel を CardLayout で切り替えるほうが分かり易いでしょうね. このようなことは経験として今後の役に立ちますので, 無駄にはならないと思います.
kbayashi

2020/04/12 11:15

励みになります。
guest

0

ダイアログの保存ボタンを押したらアクティビティにデータを渡している仕組みと同じように, 出来るのではないでしょうか.

追記:
ご質問の編集履歴によれば, ReminderSelectFragment には onSelectedReminderButton というインターフェースがあり, ReminderDialog がそれを実装している状態だったようですが, なぜ止められたのでしょう.

投稿2020/04/08 17:38

編集2020/04/08 18:09
jimbe

総合スコア12545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kbayashi

2020/04/09 10:31

回答ありがとうございます。 ReminderSelectFragmentにonAttachメソッドで、引数のcontextがonSelectedReminderButtonリスナーを実装しているなら、listener変数に格納するという処理を書いていたのですが、その引数がReminderDialogではなく、TaskAddActivityのcontextが渡されていたので、フラグメントのcontextは渡すことが出来ないのかなと思い、実装するのを辞めました。
jimbe

2020/04/09 11:49

> その引数がReminderDialogではなく、TaskAddActivityのcontextが渡されていた なるほど, 確かにそれでは使えないですね...私も ReminderDialog と思って回答しましたが, 再調査してみます.
jimbe

2020/04/09 12:00

TargetFragment がイマイチですので, さらっと行うなら getParentFragment でしょうか.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問