我正在调用一个带有以下参数的对话框:
MyDialog("title", "message").show(this@MyActivity.supportFragmentManager, null)
这是我的对话框类:
class MyDialog(private val theTitle: String, private val theMessage: String) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val myBuilder = AlertDialog.Builder(it)
myBuilder
.setTitle(theTitle)
.setMessage(theMessage)
.setPositiveButton("OK") { _, _ -> }
myBuilder.create()
}
}
}
但是,当设备的方向随着旋转而改变时,该应用将停止工作。 如果不传递任何参数,则不会发生这种情况。 那么,如何传递参数以及这样做的最佳方法是什么?
答案 0 :(得分:1)
如果它是一个片段,则应该始终有一个默认的构造函数。
单独传递参数将确保在片段的状态更改之间保留参数。
因此,有一个方法setArgument(Bundle)可在其中传递参数。
因此,此处的通话应改写为
class MyDialog: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val arg = arguments
// Use the parameters by accessing the key from variable "arg"
val myBuilder = AlertDialog.Builder(it)
myBuilder
.setTitle(theTitle)
.setMessage(theMessage)
.setPositiveButton("OK") { _, _ -> }
myBuilder.create()
}
}
}
您这样称呼Dialog:
val d = MyDialog()
val b = Bundle()
b.putInt("KEY1",1)
d.arguments = b
d.show(FragmentManager,Tag)
对于任何片段,请始终记住使用参数来传递数据
答案 1 :(得分:1)
使用Kotlin的完整解决方案
第1步。按照以下步骤创建课程
class MyDialog : DialogFragment() {
private var title: String? = null
private var message: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
title = it.getString(ARG_TITLE)
message = it.getString(ARG_MESSAGE)
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity.let {
val myBuilder = AlertDialog.Builder(it)
myBuilder
.setTitle(title)
.setMessage(message)
.setPositiveButton("OK") { _, _ -> }
myBuilder.create()
}
}
companion object {
const val TAG = "myDialog"
private const val ARG_TITLE = "argTitle"
private const val ARG_MESSAGE = "argMessage"
fun newInstance(title: String, message: String) = MyDialog().apply {
arguments = Bundle().apply {
putString(ARG_TITLE, title)
putString(ARG_MESSAGE, message)
}
}
}
}
第2步。创建实例并显示它
MyDialog.newInstance("title", "message").show(this@MyActivity.supportFragmentManager, MyDialog.TAG)
全部!
答案 2 :(得分:0)
尝试此代码,并在我传递消息时传递任何数据作为参数...
private void confirmdialog(String msg_str) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v1 = li.inflate(R.layout.dialog_forsurity, null, false);
dialog.setContentView(v1);
dialog.setCancelable(true);
TextView msg = (TextView) v1.findViewById(R.id.msg);
msg.setText(msg_str);
Button btn_submit = (Button) v1.findViewById(R.id.btn_submit);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent intent = new Intent(SellNumberPlateActivity.this, HomeActivity.class);
startActivity(intent);
finishAffinity();
}
});
dialog.show();
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
R.layout.dialog_forsurity是您对话框的设计...
答案 3 :(得分:0)
之所以要通过bundle传递参数,是因为当系统还原片段(例如,更改配置)时,它将自动还原您的bundle。
来源:https://stackoverflow.com/a/16042750/619673
代替创建DialogFragment-您应该通过从其类中调用静态方法来实例化:
public static MyDialog newInstance(String param1) {
MyDialog d = new MyDialog ();
Bundle args = new Bundle();
args.putString("param1", param1);
d.setArguments(args);
return d;
}
当您想显示它时,请致电:
MyDialog dialog = MyDialog .newInstance("lorem ipsum");
dialog.show(fm, "fragment_confirm_dialog");