Xamarin.android 如何在dialogfragment与activity之间传递数据
我想通过按钮内的一个命令将数据从dialogfragment发送到activityXamarin.android 如何在dialogfragment与activity之间传递数据,xamarin.android,Xamarin.android,我想通过按钮内的一个命令将数据从dialogfragment发送到activity //code in dialogfragment ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context); ISharedPreferencesEditor editor = prefs.Edit(); editor.PutS
//code in dialogfragment
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("my_data", "some_data");
editor.Apply();
second = new SecondActivity();
String myData = prefs.GetString("my_data", "");
second.txtView.Text = myData; // textview in secondactivity
Dismiss();
根据您的描述,您需要首先在项目中创建Dialogfragment,然后在DialogmentFragment中加载布局
public class DialogFragment1: DialogFragment
{
private TextView tv1;
private TextView tv2;
private Button btn1;
public static DialogFragment1 NewInstance(Bundle bundle)
{
DialogFragment1 fragment = new DialogFragment1();
fragment.Arguments = bundle;
return fragment;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.Signuplayout, container, false);
tv1 = view.FindViewById<TextView>(Resource.Id.textView1);
tv2= view.FindViewById<TextView>(Resource.Id.textView2);
btn1 = view.FindViewById<Button>(Resource.Id.button1);
btn1.Click += Btn1_Click;
return view;
}
private void Btn1_Click(object sender, EventArgs e)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Context);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("my_data", tv1.Text);
editor.Apply();
DialogFragment1 _exportFragment = (DialogFragment1)FragmentManager.FindFragmentByTag("Dialog Fragment");
if (_exportFragment != null)
{
_exportFragment.Dismiss();
}
}
}
然后实例化一个Mainactivity,调用DialogFragment.cs中的getdata方法:
static MainActivity mactivity;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.Signuplayout, container, false);
tv1 = view.FindViewById<TextView>(Resource.Id.textView1);
tv2= view.FindViewById<TextView>(Resource.Id.textView2);
btn1 = view.FindViewById<Button>(Resource.Id.button1);
btn1.Click += Btn1_Click;
mactivity = new MainActivity();
return view;
}
private void Btn1_Click(object sender, EventArgs e)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Context);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("my_data", tv1.Text);
editor.Apply();
DialogFragment1 _exportFragment = (DialogFragment1)FragmentManager.FindFragmentByTag("Dialog Fragment");
if (_exportFragment != null)
{
_exportFragment.Dismiss();
}
mactivity.getdata();
}
静态维护活动;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
//使用此选项可返回此片段的自定义视图
视图=充气机。充气(Resource.Layout.Signuplayout,container,false);
tv1=view.findviewbyd(Resource.Id.textView1);
tv2=view.findviewbyd(Resource.Id.textView2);
btn1=view.findviewbyd(Resource.Id.button1);
btn1.点击+=btn1\U点击;
mactivity=新的主活动();
返回视图;
}
私有无效Btn1_单击(对象发送方,事件参数e)
{
iSharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(上下文);
iSharedReferencesEditor=prefs.Edit();
编辑器.PutString(“我的_数据”,tv1.Text);
editor.Apply();
DialogFragment1_exportFragment=(DialogFragment1)FragmentManager.FindFragmentByTag(“对话片段”);
如果(_exportFragment!=null)
{
_exportFragment.disclose();
}
mactivity.getdata();
}
您的选择来自“一个命令”?数据传输过程只需单击一个按钮即可完成。我要解决的问题是,当我退出对话框时,变量add and get出现在mainactivity中,而没有按钮get The saved value。@ALIEA您的意思是想在DistrogFragment Disclose时显示数据,不需要单击按钮来获取它吗?如果是,请查看我的更新。
private Button btn1;
private Button btn2;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
btn1 = FindViewById<Button>(Resource.Id.button1);
btn2 = FindViewById<Button>(Resource.Id.button2);
btn1.Click += Btn1_Click;
btn2.Click += Btn2_Click;
}
private void Btn2_Click(object sender, System.EventArgs e)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
string myData = prefs.GetString("my_data", "");
}
private void Btn1_Click(object sender, System.EventArgs e)
{
FragmentTransaction transcation = FragmentManager.BeginTransaction();
DialogFragment1 signup = new DialogFragment1();
signup.Show(transcation, "Dialog Fragment");
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" android:text="Sign up"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="get data"
android:id="@+id/button2"/>
</LinearLayout>
public void getdata()
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Application.Context);
string myData = prefs.GetString("my_data", "");
Toast.MakeText(Application.Context,myData,ToastLength.Long).Show();
}
static MainActivity mactivity;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.Signuplayout, container, false);
tv1 = view.FindViewById<TextView>(Resource.Id.textView1);
tv2= view.FindViewById<TextView>(Resource.Id.textView2);
btn1 = view.FindViewById<Button>(Resource.Id.button1);
btn1.Click += Btn1_Click;
mactivity = new MainActivity();
return view;
}
private void Btn1_Click(object sender, EventArgs e)
{
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Context);
ISharedPreferencesEditor editor = prefs.Edit();
editor.PutString("my_data", tv1.Text);
editor.Apply();
DialogFragment1 _exportFragment = (DialogFragment1)FragmentManager.FindFragmentByTag("Dialog Fragment");
if (_exportFragment != null)
{
_exportFragment.Dismiss();
}
mactivity.getdata();
}