如何将参数传递给 android 中的对话框?

How to pass arguments to dialog in android?

我正在使用如下参数调用对话框:

MyDialog("title", "message").show(this@MyActivity.supportFragmentManager, null)

这是我的对话框class:

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()
        }
    }
}

但是当设备旋转方向改变时,应用程序停止工作。 如果没有传递任何参数,则不会发生这种情况。 那么,如何传递参数,最好的方法是什么?

如果它是一个片段,那么应该总是有一个可用的默认构造函数。 单独传递参数将确保参数在片段的状态变化中得到保留
所以有一个方法 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)

对于任何片段,永远记得使用参数来传递数据

尝试此代码并将任何数据作为参数传递,就像我传递消息一样...

    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是你的对话框设计...

The reason why you should be passing parameters through bundle is because when the system restores a fragment (e.g on config change), it will automatically restore your bundle.

来源:

而不是创建 DialogFragment - 您应该通过从它调用静态方法来实例化 class:

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");

来源:

使用 kotlin 的完整解决方案

第 1 步。 创建您的 class 关注

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)

全部!