我在哪里可以用片段编写java代码?

时间:2013-07-26 14:08:09

标签: android

我是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);
            }


        }



    }


}

抱歉我的英语不好! :)

1 个答案:

答案 0 :(得分:2)

您可以在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;
}
}