gpt4 book ai didi

android - 将参数从第二个对话 fragment 传递到第一个对话 fragment

转载 作者:行者123 更新时间:2023-11-29 17:30:40 25 4
gpt4 key购买 nike

我有两个对话 fragment

我在第一个 dialog fragment 中有一个 TextView,当用户点击它时,将带您到第二个 dialog fragment,其中用户在 EditText 中添加评论。当用户完成输入评论后,单击done 按钮返回到第一个dialog fragment。到目前为止一切顺利。

我的问题是如何将参数(用户评论)传递给第一个dialog fragment textView

第一个 DialogFragment

namespace MyApp
{
public class Report:DialogFragment
{
String comment;
public Report ()
{
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);

var view = inflater.Inflate(Resource.Layout.Report, container, false);
var textView = view.FindViewById<TextView> (Resource.Id.comment);

textView.Click += TextView_Click;
return view;
}

void TextView_Click (object sender, EventArgs e)
{
ShowDialog ();
}

public void ShowDialog()
{
var transaction = FragmentManager.BeginTransaction();
var dialogFragment = new Comment();
dialogFragment.Show(transaction, "dialog_fragment");
}
}
}

第二个 DialogFragment

namespace MyApp
{
public class Comment:DialogFragment
{

EditText comment;

public Comment ()
{
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);

var view = inflater.Inflate(Resource.Layout.Comment, container, false);

comment = view.FindViewById<EditText> (Resource.Id.myText);

Button doneBtn = view.FindViewById<Button> (Resource.Id.doneBtn);
doneBtn.Click += DoneBtn_Click;
return view;
}

void DoneBtn_Click (object sender, EventArgs e)
{
String str = comment.Text;
Console.WriteLine (str);
this.Dismiss ();
}
}
}

最佳答案

我得到了答案:

我在第二个对话框 fragment 中添加了以下代码:

void DoneBtn_Click (object sender, EventArgs e)
{
String str = comment.Text;
// the following lines are added
Bundle args = new Bundle ();
args.PutString ("comment", str);
var transaction = FragmentManager.BeginTransaction();
var dialogFragment = new Report();
dialogFragment.Arguments= args;
dialogFragment.Show(transaction, "dialog_fragment");
this.Dismiss ();
}

我在第一个对话框 fragment 中添加了以下代码:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// the following lines are added
Bundle args = Arguments;
if (args != null) {
String returnString = args.GetString ("comment");
}
}

void TextView_Click (object sender, EventArgs e)
{
ShowDialog ();
// dismiss the first dialogFragment
this.Dismiss ();
}

关于android - 将参数从第二个对话 fragment 传递到第一个对话 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33153834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com