3

我是 android 的初学者,我想制作标签,我拿了样本并对其进行了编辑,以便为每个标签制作布局,但我找不到在代码中放置每个标签的每个布局的按钮的侦听器的位置!

public class Game extends FragmentActivity implements ActionBar.TabListener {

    static int Tab_no ;



    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);


        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setTag("round").setText("Round").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setTag("score").setText("Score").setTabListener(this));
     }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) { // 3ashan law rege3na men ay makan yrg3ny le mkany
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                    savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
        }
    }

   @Override
    public void onSaveInstanceState(Bundle outState) {  // 3ashan law killed yrg3ny tany le makany
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM,
                getActionBar().getSelectedNavigationIndex());
    }



    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the container
         Fragment fragment = new SectionFragment();
         Tab_no =  tab.getPosition() + 1 ;
         getSupportFragmentManager().beginTransaction()
                 .replace(R.id.container, fragment)
                 .commit();

    }

    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A dummy fragment representing a section of the app, but that simply displays dummy text.
     */
    public static class SectionFragment extends Fragment {
        public SectionFragment() {
        }




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




            if (Tab_no==1)
            {


            return inflater.inflate(R.layout.round , container, false);


            }

            else {

            return inflater.inflate(R.layout.score , container, false);
            }


        }



    }


}

对不起我的英语不好!:)

4

1 回答 1

4

你可以在里面写onCreateView()

public class fragmentname extends Fragment{
Activity referenceActivity;
View parentHolder;

Button backBtn;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    referenceActivity = getActivity();
    parentHolder = inflater.inflate(R.layout.yourlayout, container,
            false);

    backBtn = (Button) parentHolder.findViewById(R.id.back_btn);

    backBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

        }
    });
    return parentHolder;
}
}
于 2013-07-26T14:24:50.137 回答