DialogFragment对话框(二) ——Fragment和Activity的通信
来源:互联网 发布:mac cd 到u盘 编辑:程序博客网 时间:2024/05/29 14:55
DialogFragment对话框(二) ——Fragment和Activity的通信
标签(空格分隔): fragment 通信 dialog android
作者:陈小默
紧接着上一节,DialogFragment对话框(一) ——创建对话框,这里通过在DialogFragment对话框回传EditText中的数据来介绍几种Fragment和Activity之间通信的方法
- DialogFragment对话框二 Fragment和Activity的通信
- 接口回调
- EventBus
准备工作,修改上一节中的布局文件,将TextView换成EditText
dialog_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/message" android:layout_width="300dp" android:layout_height="wrap_content" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" /></LinearLayout>
接口回调
最古老也是最原始的通信方式
定义一个作为回调的接口
Callback.kt
interface Callback { fun response(message:String)}
在Activity中传递回调接口参数
private fun showViewDialog() { var dialog: MyViewDialogFragment = MyViewDialogFragment() var bundle = Bundle() bundle.putSerializable("callback", object : Callback { override fun response(message: String) { Toast.makeText(this@ShowActivity, message, Toast.LENGTH_SHORT).show() } }) dialog.arguments=bundle dialog.show(fragmentManager, "tag") }
在Fragment中接收参数
class MyViewDialogFragment : DialogFragment() { private var callback: Callback? = null override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题 callback = arguments["callback"] as Callback//获得Activity传来的回调接口 var view: View = inflater.inflate(R.layout.dialog_view, container, false) initView(view) return view } override fun show(manager: FragmentManager?, tag: String?) { super.show(manager, tag) } private fun initView(view: View) { var message = view.findViewById(R.id.message) as EditText var button = view.findViewById(R.id.ok) as Button button.setOnClickListener { view -> callback?.response(message.text.toString()) dismiss() } }}
这时候再运行,我们就能看到Activity中吐司的对话框中输入的文字了
EventBus
使用EventBus传递数据应该是最简便的方法了
修改Activity的代码
class ShowActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_show) findViewById(R.id.alertDialog).setOnClickListener { view -> showAlertDialog() } findViewById(R.id.viewDialog).setOnClickListener { view -> showViewDialog() } EventBus.getDefault().register(this) } private fun showAlertDialog() { var dialog: MyAlertDialogFragment = MyAlertDialogFragment() dialog.ctx = this dialog.show(fragmentManager, "tag") } private fun showViewDialog() { var dialog: MyViewDialogFragment = MyViewDialogFragment() dialog.show(fragmentManager, "tag") } @Subscribe open fun response(message:String){ Toast.makeText(this@ShowActivity, message, Toast.LENGTH_SHORT).show() } override fun onDestroy() { EventBus.getDefault().unregister(this) super.onDestroy() }}
修改Fragment的代码
class MyViewDialogFragment : DialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)//设置窗体为无标题 var view: View = inflater.inflate(R.layout.dialog_view, container, false) initView(view) return view } override fun show(manager: FragmentManager?, tag: String?) { super.show(manager, tag) } private fun initView(view: View) { var message = view.findViewById(R.id.message) as EditText var button = view.findViewById(R.id.ok) as Button button.setOnClickListener { view -> EventBus.getDefault().post(message.text.toString()) dismiss() } }}
0 0
- DialogFragment对话框(二) ——Fragment和Activity的通信
- 在onActivityResult中弹出DialogFragment的解决方案 activity和fragment
- activity和fragment的通信
- Fragment与Activity的相互通信(二)
- fragment和activity通信
- Activity和Fragment通信
- Fragment初学3——使用Fragment的子类DialogFragment
- Fragment和Activity之间的相互通信
- Activity和Fragment之前的通信
- 关于activity和fragment的通信
- Fragment和Activity之间的相互通信
- Activity和Fragment之间的通信
- Fragment和Activity之间的通信
- Fragment和Activity之间的通信
- Android入门——Fragment详解之Fragment与Activity通信及数据交换(二)
- Android中的Fragment ---- 04(Activity和Fragment之间的通信)
- Android中的Fragment ---- 05(Activity和Fragment之间的通信)
- DialogFragment对话框(一) ——创建对话框
- 视频测试
- 怎么成为一个微信开发者
- 动态规划背包问题
- 在SqlServer中,教你如何修改列名
- [leetcode] 257. Binary Tree Paths
- DialogFragment对话框(二) ——Fragment和Activity的通信
- poj 3258 River Hopscotch(二分最大化最小值)
- Linux常用命令全解
- Spring Boot:在Spring Boot中使用定时任务
- vim中:wq和:x的区别
- 【HDU】5256 - 序列变换(LIS)
- Java文件读写操作
- Gson解析Map格式json数据
- hdu 5762 Teacher Bo(vector的使用1)