需要帮助,我一直在尝试寻找一种方法,以便可以将数据从kotlin传递到DialogFragment的newInstance。 下面的kotlin代码
private var messageDialog: MessageDialog? = null
override fun onLoginFail(value: String?) {
//TODO:find hpw to implement this
messageDialog = MessageDialog.newInstance(getString(R.string.sign_title),getString(R.string.signin_msg,value)
messageDialog?.show(supportFragmentManager,null)
}
下面的对话框片段
public static MessageDialog newInstance(String title, String msg){
Bundle args = new Bundle();
args.putString(EXTRA_MSG,msg);
args.putString(EXTRA_TITLE,title);
MessageDialog messageDialog = new MessageDialog();
messageDialog.setArguments(args);
return messageDialog;
}
答案 0 :(得分:2)
在kotlin中,我们使用伴随对象来制作静态方法,这是您可以根据需要执行的操作:
在消息对话框中:
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Message Dialog Fragment.
*/
@JvmStatic
fun newInstance(title: String, msg: String) = MessageDialog().apply {
arguments = Bundle().apply {
putString(EXTRA_TITLE, tile)
putString(EXTRA_MSG, msg)
}
}
}
在消息对话框片段中,onCreate
方法:
var title =""
var msg = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
title= it.getString(EXTRA_TITLE)
msg = it.getString(EXTRA_MSG)
}
}
现在您可以致电MessageDialog.newInstance("Sample title","Sample msg")