2017-11-15 2 views
0

私のアプリに問題があります。まず、フラグメントを使用して2つのタブを作成し、アクティビティを展開しました。実装されたタブは正常に動作しています。第二に私はXAMLを表示しています。しかし、ボタンと対話することはできませんが、テキストフィールドに書き込むことは可能です。しかし、ボタンでそれらを更新しようとすると、テキストフィールドは応答しません。ボタンクリックに応答しないフラグメント

アクティビティは、フラグメントを使用しないで正しく動作します。私は何ができるのですか?どんな助けでも大歓迎です!おかげ

ここでは私のコードです:


MainActivityコード:

public class TabActivity : AppCompatActivity 
    { 
     private DrawerLayout mDrawerLayout; 
     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      SetContentView(Resource.Layout.Main); 

      //Toolbar 
      SupportToolbar toolbar = FindViewById<SupportToolbar>(Resource.Id.toolBar); 
      SetSupportActionBar(toolbar); 

      //ActionBar 
      SupportActionBar actionBar = SupportActionBar; 
      actionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu); 
      actionBar.SetDisplayHomeAsUpEnabled(true); 
      //Drawer 
      mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout); 
      NavigationView navigationView = FindViewById<NavigationView>(Resource.Id.nav_view); 
      if(navigationView != null) 
      { 
       setUpDrawerContent(navigationView); 
      } 
      //Tabs 
      TabLayout tabs = FindViewById<TabLayout>(Resource.Id.tabs); 

      ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.viewpager); 
      setUpViewPager(viewPager); 

      tabs.SetupWithViewPager(viewPager); 
      }; 
    } 


    // What Actually sets my tabs 
     private void setUpViewPager(ViewPager viewPager) 
     { 
     //==================================== 
      //SET THE FRAGMENTS HERE. 
     //==================================== 
      TabAdapter adapter = new TabAdapter(SupportFragmentManager); 
      adapter.AddFragment(new Fragment1(), "Inventar"); 
      adapter.AddFragment(new Fragment2(), "Onskeliste"); 

      viewPager.Adapter = adapter; 
     } 
     public override bool OnOptionsItemSelected(IMenuItem item) 
     { 
      switch (item.ItemId) 
      { 
       //Checks if the drawer is opened all the way up 
       case Android.Resource.Id.Home: 
        mDrawerLayout.OpenDrawer((int)GravityFlags.Left); 
        return true; 
       default: 
        return base.OnOptionsItemSelected(item); 
      } 
     } 
     private void setUpDrawerContent(NavigationView navigationView) 
     { 
      navigationView.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) => 
      { 
       e.MenuItem.SetChecked(true); 
       mDrawerLayout.CloseDrawers(); 
      }; 
     } 

     //=================================== 
     // Creating a tab adapter which inherrit from a fragment adaper, is used to adapt the viewpager and tabs together 
     //=================================== 
     public class TabAdapter : FragmentPagerAdapter 
     { 

      public List<SupportFragment> Fragments { get; set; } 
      public List<string> FragmentNames { get; set; } 

      public TabAdapter (SupportoFragmentManager sfm) : base (sfm) 
      { 
       Fragments = new List<SupportFragment>(); 
       FragmentNames = new List<string>(); 
      } 

      public void AddFragment(SupportFragment fragment, string name) 
      { 
       Fragments.Add(fragment); 
       FragmentNames.Add(name); 
      } 

      public override int Count 
      { 
       get 
       { 
        return Fragments.Count; 
       } 
      } 

      //Gets the item of the fragemnt at the specific possition 
      public override SupportFragment GetItem(int position) 
      { 
       return Fragments[position]; 
      } 

      //Sets the title of the fragment 
      public override ICharSequence GetPageTitleFormatted(int position) 
      { 
       return new Java.Lang.String(FragmentNames[position]); 
      } 
     } 
    }` 

フラグメント1

public class Fragment1 : SupportFragment 
    { 
     public override void OnInflate(Activity activity, IAttributeSet attrs, Bundle savedInstanceState) 
     { 
      base.OnInflate(activity, attrs, savedInstanceState); 
     } 

     public override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
     } 

     public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { 
      //Inflates the view under the tabs 
      View view = inflater.Inflate(Resource.Layout.Inventory, container, false); 

      return view; 
     } 
    } 

XMLファイル

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/scroll2" 
     android:padding="16dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FBC855"> 
     <RelativeLayout 
      android:id="@+id/activity_my_cellar_page" 
      android:padding="16dp" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:focusable="true" 
      android:focusableInTouchMode="true"> 
      <android.support.design.widget.TextInputLayout 
       android:layout_below="@+id/signup_color" 
       android:id="@+id/signup_number" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       <EditText 
        android:id="@+id/number" 
        android:hint="Enter the amount of beer" 
        android:inputType="textCapWords" 
        android:maxLines="1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
      </android.support.design.widget.TextInputLayout> 
      <LinearLayout 
       android:layout_below="@+id/signup_number" 
       android:id="@+id/registration" 
       android:gravity="center" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       <Button 
        android:id="@+id/Btn_lagre" 
        android:text="Lagre" 
        android:background="#6C2334" 
        style="@style/Widget.AppCompat.Button.Colored" 
        android:layout_width="50dp" 
        android:layout_height="35dp" /> 
      </LinearLayout> 
      <ListView 
       android:id="@+id/list_data" 
       android:layout_below="@+id/registration" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 
      <ProgressBar 
       android:layout_below="@+id/list_data" 
       android:id="@+id/circularProgress" 
       android:visibility="invisible" 
       android:layout_centerInParent="true" 
       android:theme="@style/CircularProgress" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/Widget.AppCompat.ProgressBar" /> 
      <LinearLayout 
       android:layout_below="@+id/circularProgress" 
       android:id="@+id/mycellar_inventory3" 
       android:gravity="center" 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
       <View 
        android:layout_width="50dp" 
        android:layout_height="1dp" 
        android:background="#6C2334" 
        android:layout_margin="5dp" /> 
       <Button 
        android:id="@+id/Btn_inventartilbake" 
        android:text="Tilbake" 
        android:background="#6C2334" 
        style="@style/Widget.AppCompat.Button.Colored" 
        android:layout_width="150dp" 
        android:layout_height="50dp" /> 
       <View 
        android:layout_width="50dp" 
        android:layout_height="1dp" 
        android:background="#6C2334" 
        android:layout_margin="5dp" /> 
      </LinearLayout> 
     </RelativeLayout> 
    </ScrollView> 
+0

XMLファイルはあなたの活動のためのものですか? –

+0

はい、xmlファイルは自分の活動用です –

答えて

0

編集:私はあなたのXMLファイルは、あなたの活動のためのものであることを前提としています。

あなたがそれらとやりとりしたいのであれば、あなたの尻を宣言する必要があります。バテンがあなたのアクティビティにある場合は、あなたのアクティビティでそれを宣言する必要があります。そうでなければ、あなたのバテンがあなたのフラグメントにある場合、それをあなたのフラグメントに宣言する必要があります..下のコードで、フラグメントのXMLで宣言さbutten - fragementを膨らませると、その後、私が膨張したフラグメントからbuttenを使用します。

public class FragmentSettings : SupportFragment 
{ 
    private Button mBtnOk; 

    public FragmentSettings(Context context) 
    { 
     mContext = context; 
    } 

    public override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     //Inflate the fragment XML 
     View view = inflater.Inflate(Resource.Layout.FragmentSettings, container, false); 

     //Grab the butten from the inflated fragment 
     mBtnOk = view.FindViewById<Button>(Resource.Id.mBtnOk); 
     mBtnOk.Click += (object sender, EventArgs e) => 
     { 
      //DO stuff 
     }; 

     return view; 
    } 
} 

はそれが役に立てば幸い - あなたが理解しない気にいらがあるかどうか尋ねますか?

+0

public FragmentSettingsはどこから来たのですか?それは何ですか? –

+0

それは私自身のプロジェクトの一つから抜粋した単なる断片ですか?私は膨らんだXMLを意味しますか? –

+0

ええ、私は今それを考え出して助けてくれてありがとう!^_^ –

関連する問題