[关闭]
@cxm-2016 2016-07-27T11:46:37.000000Z 字数 3262 阅读 3422

DialogFragment对话框(二) ——Fragment和Activity的通信

fragment 通信 dialog android
作者:陈小默


紧接着上一节,DialogFragment对话框(一) ——创建对话框,这里通过在DialogFragment对话框回传EditText中的数据来介绍几种Fragment和Activity之间通信的方法

准备工作,修改上一节中的布局文件,将TextView换成EditText
dialog_view.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical">
  5. <EditText
  6. android:id="@+id/message"
  7. android:layout_width="300dp"
  8. android:layout_height="wrap_content" />
  9. <Button
  10. android:id="@+id/ok"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="确定" />
  14. </LinearLayout>

接口回调

最古老也是最原始的通信方式
定义一个作为回调的接口
Callback.kt

  1. interface Callback {
  2. fun response(message:String)
  3. }

在Activity中传递回调接口参数

  1. private fun showViewDialog() {
  2. var dialog: MyViewDialogFragment = MyViewDialogFragment()
  3. var bundle = Bundle()
  4. bundle.putSerializable("callback", object : Callback {
  5. override fun response(message: String) {
  6. Toast.makeText(this@ShowActivity, message, Toast.LENGTH_SHORT).show()
  7. }
  8. })
  9. dialog.arguments=bundle
  10. dialog.show(fragmentManager, "tag")
  11. }

在Fragment中接收参数

  1. class MyViewDialogFragment : DialogFragment() {
  2. private var callback: Callback? = null
  3. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  4. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题
  5. callback = arguments["callback"] as Callback//获得Activity传来的回调接口
  6. var view: View = inflater.inflate(R.layout.dialog_view, container, false)
  7. initView(view)
  8. return view
  9. }
  10. override fun show(manager: FragmentManager?, tag: String?) {
  11. super.show(manager, tag)
  12. }
  13. private fun initView(view: View) {
  14. var message = view.findViewById(R.id.message) as EditText
  15. var button = view.findViewById(R.id.ok) as Button
  16. button.setOnClickListener { view ->
  17. callback?.response(message.text.toString())
  18. dismiss()
  19. }
  20. }
  21. }

这时候再运行,我们就能看到Activity中吐司的对话框中输入的文字了

EventBus

使用EventBus传递数据应该是最简便的方法了
修改Activity的代码

  1. class ShowActivity : AppCompatActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.activity_show)
  5. findViewById(R.id.alertDialog).setOnClickListener { view -> showAlertDialog() }
  6. findViewById(R.id.viewDialog).setOnClickListener { view -> showViewDialog() }
  7. EventBus.getDefault().register(this)
  8. }
  9. private fun showAlertDialog() {
  10. var dialog: MyAlertDialogFragment = MyAlertDialogFragment()
  11. dialog.ctx = this
  12. dialog.show(fragmentManager, "tag")
  13. }
  14. private fun showViewDialog() {
  15. var dialog: MyViewDialogFragment = MyViewDialogFragment()
  16. dialog.show(fragmentManager, "tag")
  17. }
  18. @Subscribe open fun response(message:String){
  19. Toast.makeText(this@ShowActivity, message, Toast.LENGTH_SHORT).show()
  20. }
  21. override fun onDestroy() {
  22. EventBus.getDefault().unregister(this)
  23. super.onDestroy()
  24. }
  25. }

修改Fragment的代码

  1. class MyViewDialogFragment : DialogFragment() {
  2. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
  3. dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题
  4. var view: View = inflater.inflate(R.layout.dialog_view, container, false)
  5. initView(view)
  6. return view
  7. }
  8. override fun show(manager: FragmentManager?, tag: String?) {
  9. super.show(manager, tag)
  10. }
  11. private fun initView(view: View) {
  12. var message = view.findViewById(R.id.message) as EditText
  13. var button = view.findViewById(R.id.ok) as Button
  14. button.setOnClickListener { view ->
  15. EventBus.getDefault().post(message.text.toString())
  16. dismiss()
  17. }
  18. }
  19. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注