片段没有响应buttonclicks

时间:2017-11-15 09:28:00

标签: c# android xml xaml android-fragments

我的应用有问题。首先,我使用片段制作了两个标签,这些标签会使活动膨胀。实现的选项卡工作正常。其次,我已经显示了 XAML 。但是,无法与按钮进行交互,但可以在文本字段中进行编写。但是,当我尝试使用按钮更新文本字段时,文本字段将不会响应。

活动正常工作不使用碎片,任何线索我能做什么?任何帮助是极大的赞赏!谢谢

这是我的代码:

MainActivity Code:

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>

1 个答案:

答案 0 :(得分:0)

编辑:我认为您的XML文件适用于您的活动。

如果你想和他们互动,你需要申报你的黄油。如果butten在你的活动中,你需要在你的活动中声明它...否则,如果你的butten在你的片段中你需要在你的片段中声明它..在下面的代码中,我有一个片段,我有在片段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;
    }
}

希望它有所帮助 - 问你是否有不明白的事情?