2015-10-15 88 views
1

我有兩個dialog fragments將參數從第二個傳遞給第一個對話框片段

我在第一dialog fragment一個TextView當用戶點擊它,即可轉到用戶添加在EditText意見第二dialog fragment。當用戶輸入他的評論完成後,點擊done按鈕返回到第一個dialog fragment。到目前爲止這麼好,工作。

我的問題是如何傳遞和分配參數(user comment)到第一個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(); 
     } 
    } 
} 
+0

這是什麼語言? – lustig

+0

你做過任何研究或嘗試過任何東西嗎?那裏有很多信息。 –

+1

這是C# - Xamarin.Android平臺 – hotspring

回答

2

我得到了答案:

我在第二個對話框片段添加以下代碼:

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

我已經在第一個對話框片段添加以下代碼:

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

我會給你一個提示。在你的第二個片段創建這樣一個方法,並從您的DoneBtn_Click稱之爲:

private void sendResult(int resultCode,Date date) { 
    if(getTargetFragment() == null) { 
     return; 
    } 

    Intent intent = new Intent(); 
    intent.putExtra(EXTRA_TIME,date); 

     getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent); 
} 

而在你的第一個片段,實現onActivityResult。這是Java,但你應該明白。

+0

嗨瑞克,但我解僱了第二個'對話框片段',你正在使用'意圖'?我已經提高了你的努力。 – hotspring

+0

沒關係,我已經得到了解決方案,並在下面添加。 – hotspring

相關問題