對不起,我正在使用谷歌翻譯。
我正在嘗試將資料從 Fragment 發送到 Activity,但沒有成功,這樣做的目的是更新 TextView。我無法驗證 Intent 是否帶來了資料,如果我消除驗證它們有一個空值,我會詳細說明我的代碼。
分段
private void sendData()
{
Intent i = new Intent(Context,Activity.Class);
//PACK DATA
i.PutExtra("COUNT", "1");
//START ACTIVITY
Activity.StartActivity(i);
}
呼叫
void ButtonClicked2(object sender, EventArgs args)
{
sendData();
}
活動
protected override void OnResume()
{
base.OnResume();
//Intent i = Intent;
Bundle sender = Intent.Extras;
////IF ITS THE FRAGMENT THEN RECEIVE DATA
if (sender != null)
{
receiveData();
Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();
}
}
private void receiveData()
{
//RECEIVE DATA VIA INTENT
Intent i = Intent;
string count = i.GetStringExtra("COUNT");
//SET DATA TO TEXTVIEWS
Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
}
uj5u.com熱心網友回復:
一個關于如何將資料從fragment傳遞到Activity的簡單示例供您參考。
片段:
我的片段:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@ id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@ id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
MyFragement.cs:
public class MyFragment : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
// return inflater.Inflate(Resource.Layout.YourFragment, container, false);
// A large amount of text to display.
var textToDisplay = "Hello";
var view = inflater.Inflate(Resource.Layout.MyFragment, container, false);
var textView = view.FindViewById<TextView>(Resource.Id.textView1);
textView.Text = textToDisplay;
var button = view.FindViewById<Button>(Resource.Id.button1);
button.Click = Button_Click;
return view;
}
private void Button_Click(object sender, EventArgs e)
{
sendData();
}
private void sendData()
{
Intent i = new Intent(Context, Activity.Class);
//PACK DATA
i.PutExtra("COUNT", "1");
//START ACTIVITY
Activity.StartActivity(i);
}
}
活動:
活動主.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/fragment_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
MainAcitivity.cs:
public class MainActivity : AppCompatActivity
{
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);
var newFragment = new MyFragment();
var ft = FragmentManager.BeginTransaction();
ft.Add(Resource.Id.fragment_container, newFragment);
ft.Commit();
}
protected override void OnResume()
{
base.OnResume();
//Intent i = Intent;
Bundle sender = Intent.Extras;
////IF ITS THE FRAGMENT THEN RECEIVE DATA
if (sender != null)
{
receiveData();
Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();
}
}
private void receiveData()
{
//RECEIVE DATA VIA INTENT
Intent i = Intent;
string count = i.GetStringExtra("COUNT");
var textView = FindViewById<Android.Widget.TextView>(Resource.Id.textView1);
textView.Text = count;
//SET DATA TO TEXTVIEWS
//Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
//CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
//Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
}
}
截屏:
uj5u.com熱心網友回復:
我能夠自己解決它,雖然我的解決方案并不能完全說服我,我會發布它以防有人和我有同樣的問題。
分段
//add the activity reference to the fragment
using static GumisaAPP.Activities.BaseActivity;
//we create the procedure that will be in charge of modifying the activity's TextView
private void sendData_CountItem()
{
string count;
Variables.Contador_Items = Variables.Contador_Items 1;
count= Convert.ToString(Variables.Contador_Items);
//we access the texview of the activity from the fragment
TextView textCount = Activity.FindViewById<TextView>(Resource.Id.itemCount);
// modify the text of the TextView
textCount.SetText(count, TextView.BufferType.Normal);
}
// call the procedure
private void EnviarDatos(){
sendData_CountItem();
}
感謝您不厭其煩地回答我的問題。問候
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/481181.html