@cxm-2016
2016-07-27T11:46:37.000000Z
字数 3262
阅读 3401
fragment
通信
dialog
android
作者:陈小默
紧接着上一节,DialogFragment对话框(一) ——创建对话框,这里通过在DialogFragment对话框回传EditText中的数据来介绍几种Fragment和Activity之间通信的方法
准备工作,修改上一节中的布局文件,将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传递数据应该是最简便的方法了
修改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()
}
}
}