选项卡式活动片段中的访问按钮

时间:2016-01-20 18:28:34

标签: android-studio tabs

抱歉英语不好..真的需要帮助!

我几天前刚开始探索android工作室,所以我还是新手。 我的问题是我无法访问我在选项卡式活动中创建的按钮。即时通讯使用android studio 1.5.1.Below是最终输出:

enter image description here

正如你所看到的,我设法将按钮放在标签内,但我不能让它们做动作,因为我不知道如何访问它们。我希望它们能够弹出我已创建的警报对话框。

以下是我所做的所有代码:

akauninduk.java



XMPPFramework




这是将所有按钮作为上图的xml文件:fragment_akauninduk.xml



package com.example.asus.gorunnerapp;


import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.Button;

public class akauninduk extends AppCompatActivity implements View.OnClickListener {

    /**
     * The {@link PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_akauninduk);




        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.


        //String string = getResources().getString(R.string.title_activity_akauninduk);

        getMenuInflater().inflate(R.menu.menu_akauninduk, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {


    }





    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;


            if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
                rootView = inflater.inflate(R.layout.fragment_akauninduk, container, false);
            } else
                rootView = inflater.inflate(R.layout.fragment_akauninduk2, container, false);


            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "PENGAGIHAN";
                case 1:
                    return "SENARAI PERMOHONAN";
            }
            return null;
        }
    }
}




我创建了一个新的java类来处理fragment_akauninduk.xml文件,我已经创建了这个函数,这样点击按钮就可以了,但是我没有工作。

这是处理fragment_akauninduk.xml文件的java类:

handletab.java



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.asus.gorunnerapp.akauninduk$PlaceholderFragment">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="108dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="JPMM"
            android:id="@+id/textView11"
            android:layout_below="@+id/section_label"
            android:layout_toRightOf="@+id/section_label"
            android:layout_toEndOf="@+id/section_label"
            android:textSize="25dp"
            android:textColor="#000000"
            android:textAlignment="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="RM15,000 / RM1000"
            android:id="@+id/textView12"
            android:textAlignment="center"
            android:textColor="#2618a1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pilih Menu"
            android:id="@+id/bButton1"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="108dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="NADI"
            android:id="@+id/textView9"
            android:layout_below="@+id/section_label"
            android:layout_toRightOf="@+id/section_label"
            android:layout_toEndOf="@+id/section_label"
            android:textSize="25dp"
            android:textColor="#000000"
            android:textAlignment="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="RM15,000 / RM1000"
            android:id="@+id/textView10"
            android:textAlignment="center"
            android:textColor="#2618a1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pilih Menu"
            android:id="@+id/bButton2"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="108dp" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="MAYANG"
            android:id="@+id/textView7"
            android:layout_below="@+id/section_label"
            android:layout_toRightOf="@+id/section_label"
            android:layout_toEndOf="@+id/section_label"
            android:textSize="25dp"
            android:textColor="#000000"
            android:textAlignment="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="RM15,000 / RM1000"
            android:id="@+id/textView8"
            android:textAlignment="center"
            android:textColor="#2618a1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pilih Menu"
            android:id="@+id/bButton3"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="108dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="UMNO"
            android:id="@+id/textView5"
            android:layout_below="@+id/section_label"
            android:layout_toRightOf="@+id/section_label"
            android:layout_toEndOf="@+id/section_label"
            android:textSize="25dp"
            android:textColor="#000000"
            android:textAlignment="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="RM15,000 / RM1000"
            android:id="@+id/textView6"
            android:textAlignment="center"
            android:textColor="#2618a1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pilih Menu"
            android:id="@+id/bButton4"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp" />
    </LinearLayout>

</LinearLayout>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你现在正在做的只是膨胀2种不同的布局,因此你无法访问和使用它。你真正需要做的是创建2个片段(新的&gt;片段&gt;片段(空白))然后在方法getItem(position)中调用它,就像下面的代码一样。然后你让每个标签在片段文件中完成它们的工作。

@Override
        public Fragment getItem(int position) {
            if(position == 0){
                 FragmentPengagihan fragmentPengagihan = new FragmentPengagihan();
                 return fragmentPengagihan;
                 }

            else if(position == 1){
                 FragmentSenaraiPermohonan fragmentPermohonan = new FragmentSenaraiPermohonan();
                 return fragmentSenaraiPermohonan;
            }

            return null;
        }
相关问题