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

相关内容

热门资讯

九分钟教学!微信小程序财神十三... 九分钟教学!微信小程序财神十三张能开挂吗,wepoke辅助挂(详细透视辅助插件教程);玩家在微信小程...
八分钟教学!牌乐门外挂透视,德... 八分钟教学!牌乐门外挂透视,德扑概率计算软件(详细透视辅助工具教程);一、牌乐门外挂透视有挂的是的,...
6分钟透明!小白大作战外 挂,... 6分钟透明!小白大作战外 挂,微扑克大厅都是机器人(详细透视辅助挂教程) 科技详细教程;757446...
6分钟辅助挂!易棋游戏有挂吗,... 6分钟辅助挂!易棋游戏有挂吗,微扑克ai技术(详细透视辅助黑科技教程)相信很多朋友都在电脑上玩过易棋...
3分钟教学!微信边锋斗地主怎么... 3分钟教学!微信边锋斗地主怎么提升胜率,wpk微扑克真的有辅助插件(详细透视辅助插件教程);微信边锋...
两分钟详情!天府之城麻将挂,w... 两分钟详情!天府之城麻将挂,wpk发牌算法(详细透视辅助工具教程)是一款可以让一直输的玩家,快速成为...
八分钟规律!微乐软件定制开发,... 八分钟规律!微乐软件定制开发,wpk线上代打(详细透视辅助助手教程)关于微乐软件定制开发的基本介绍、...
九分钟介绍!掌心娱乐辅助器,云... 九分钟介绍!掌心娱乐辅助器,云扑克app辅助(详细透视辅助助手教程) 科技详细教程;75744690...
四分钟技巧!越乡游辅助器,德州... 四分钟技巧!越乡游辅助器,德州wepower透视辅助(详细透视辅助工具教程)是一款可以让一直输的玩家...
七分钟透明!WPK透视辅助,w... WePoker透视辅助工具核心要点解析‌,七分钟透明!WPK透视辅助,wepower系统规律(详细透...