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

相关内容

热门资讯

攻略讲解!边锋老友棋牌能装挂,... 攻略讲解!边锋老友棋牌能装挂,德州苹果版2024最新消息官网,详细有挂(有挂规律)-哔哩哔哩;(需添...
8分钟了解(约局吧软件)外挂透... 8分钟了解(约局吧软件)外挂透明挂辅助机制(外挂)发牌规律(2022已更新)(哔哩哔哩);原来确实真...
透明教学!雀神微信小程序免费辅... 1、透明教学!雀神微信小程序免费辅助器教程(辅助挂)太坑了竟然是真的有挂(有挂详情)-哔哩哔哩;该软...
科普!边锋老友棋牌免费挂(wE... 科普!边锋老友棋牌免费挂(wEpOke),太坑了的确真的有挂(有挂攻略)-哔哩哔哩是一款可以让一直输...
透视美元局!边锋老友麻将真的有... 《透视美元局!边锋老友麻将真的有挂,wepoker透视挂,详细有挂(有挂介绍)-哔哩哔哩》 边锋老友...
盘点十款(微扑克数据)外挂透明... 微扑克数据高级策略深度解析‌;盘点十款(微扑克数据)外挂透明挂辅助工具(外挂)软件透明挂(2023已...
查到实测!雀神小程序辅助买(透... 查到实测!雀神小程序辅助买(透视)太坑了原来是真的有挂(有挂教程)-哔哩哔哩是一款可以让一直输的玩家...
盘点几款!边锋麻将有挂,wep... 盘点几款!边锋麻将有挂,wepoker私人局有挂,详细有挂(有挂技巧)-哔哩哔哩;玩家必备必赢加哟《...
透视脚本(aapokEr)外挂... 透视脚本(aapokEr)外挂透明挂辅助神器(系统)发牌规律(2024已更新)(哔哩哔哩);建议优先...
必备科技!雀神广东麻将的输赢机... 必备科技!雀神广东麻将的输赢机制(透视)太坑了确实真的有挂(有挂透明)-哔哩哔哩;玩家必备必赢加哟《...