0

我真的没想到一个好问题的标题。对于那个很抱歉。FragmentActivity和Fragment ...我如何在这种情况下工作?

这里是我的问题: 我有我的主要活动:

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Support.V4.App; 

    namespace AndroidApplication2 
    { 
     [Activity(Label = "AndroidApplication2", MainLauncher = true, Icon = "@drawable/icon")] 
     public class Activity1 : FragmentActivity 
     { 
      protected override void OnCreate(Bundle bundle) 
      { 
       base.OnCreate(bundle); 

       // Set our view from the "main" layout resource 
       SetContentView(Resource.Layout.Main);   
       //Not sure if I need the code below 
       FragmentTransaction ft = this.SupportFragmentManager.BeginTransaction(); 
       Activity2 fragment = new Activity2(); 
       ft.Add(Resource.Id.fragmentContainer, fragment); 
       ft.Commit(); 
      } 
     } 
    } 

为主要活动的XML文件看起来像:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <fragment 
     class="AndroidApplication2.Activity2" 
     android:id="@+id/fragmentContainer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <Button 
     android:id="@+id/changeFragment" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Change Fragment" /> 
</LinearLayout> 

现在,活性2看起来是这样的:

namespace AndroidApplication2 
{ 
    [Activity(Label = "My Activity")] 
    public class Activity2 : Fragment 
    { 
     public override View OnCreateView(LayoutInflater p0, ViewGroup p1, Bundle p2) 
     { 
      return p0.Inflate(Resource.Layout.fragment_layout, p1, false); 
     } 
    } 
} 

和fragment_layout.axml看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/myTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is a TextView from the fragment" /> 
    <Button 
     android:id="@+id/myButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is a Button from the fragment" /> 
</LinearLayout> 

此代码是在C#中使用Xamarin。

现在,我想要做的就是访问fragment_layout中的按钮并使用它来替换Activity1中的片段(我的主要活动)。我正在使用它来避免为每个简单的事情创建新的活动,例如显示某些细节,但我也不能使用任何弹出窗口。

我无法访问我在我的主FragmentActivity中加载的片段上的按钮,我可以用它替换另一个片段。帮帮我!谢谢!任何其他改进或建议也是受欢迎的。

+0

好的,我的一个问题就解决了。如果静态添加,我无法替换片段。但我仍然不知道如何访问我添加的新片段的按钮。请帮助我。谢谢! –

回答

0

“但我仍然不知道如何访问我添加的新片段的按钮。”

那么你总是可以参考该按钮是这样的:

_btnAboutUs = (Button)FindViewById (Resource.Id.btnAboutUs); 

但也许使用片段中的按钮,以取代片段按钮本身是,是不是最好的一段路要走。 为什么不把按钮放在Activity1中,以便您可以使用它的fragmentManager来替换片段。一个片段没有fragmentManager,但是一个活动。

1

尝试先通过膨胀视图来访问按钮,然后返回它。 例如:

View view = p0.Inflate(Resource.Layout.fragment_layout, p1, false); 

// now we can access button, textview etc... in the layout 

_btnAboutUs = (Button)view.FindViewById(Resource.Id.btnAboutUs); 

return view; 
相关问题