Androidstudio中如何将对话框中的数据传递到另一个对话框?
创始人
2024-08-21 02:30:13
0

在对话框中使用Bundle传递数据。

例如,假设您有两个对话框:第一个对话框显示一个EditText,第二个对话框显示第一个对话框中输入的文本。

在第一个对话框的onClick方法中,创建一个Bundle并将文本添加到该Bundle:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
EditText input = new EditText(getActivity());
builder.setTitle("Enter Text");
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // Get the entered text from the EditText
        String text = input.getText().toString();
        // Create a bundle to pass to the second dialog
        Bundle bundle = new Bundle();
        bundle.putString("text", text);
        // Create the second dialog and pass the bundle to it
        SecondDialogFragment secondDialog = new SecondDialogFragment();
        secondDialog.setArguments(bundle);
        secondDialog.show(getFragmentManager(), "SecondDialog");
    }
});
builder.setNegativeButton("Cancel", null);
builder.show();

然后,在第二个对话框的onCreateDialog方法中,获取Bundle中的文本并将其添加到第二个对话框的视图中:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    Bundle bundle = getArguments();
    String text = bundle.getString("text");
    builder.setTitle("Second Dialog");
    builder.setMessage(text);
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    return builder.create();
}

相关内容

热门资讯

透视真的!aapoker破解侠... 透视真的!aapoker破解侠是真的吗(透视)辅助插件工具(确实有挂);1、进入游戏-大厅左侧-新手...
透视ai!aapoker脚本(... 透视ai!aapoker脚本(透视)透视插件(果然存在有挂)1、任何aapoker脚本ai辅助神器的...
透视苹果版!aapoker辅助... 透视苹果版!aapoker辅助插件工具(透视)透视软件(本来存在有挂);亲,关键说明,aapoker...
透视数据!aapoker公共底... 透视数据!aapoker公共底牌(透视)透视软件(本来是有挂)1、完成aapoker公共底牌透视辅助...
透视ai!aapoker俱乐部... 透视ai!aapoker俱乐部靠谱吗(透视)辅助插件工具(真是真的有挂)1、这是跨平台的aapoke...
透视讲解!aapoker怎么控... 透视讲解!aapoker怎么控制牌(透视)透视脚本(一贯是真的有挂);1、透视讲解!aapoker怎...
辅助透视!aapoker免费透... 辅助透视!aapoker免费透视脚本(透视)透视脚本(总是存在有挂)1、每一步都需要思考,不同水平的...
透视免费!aapoker插件(... 透视免费!aapoker插件(透视)插件(果然是有挂)1、在aapoker插件ai机器人技巧中,中转...
透视插件!aapoker怎么选... 透视插件!aapoker怎么选牌(透视)免费透视脚本(总是是有挂);1、aapoker怎么选牌ai辅...
透视ai代打!aapoker能... 透视ai代打!aapoker能控制牌吗(透视)透视方法(本来存在有挂);1、下载好aapoker能控...