2013-07-26 34 views
2

iam在Android的初学者 我想制作标签 我拿了样本,并编辑它,使每个标签 布局,但我找不到在哪里把每个标签的每个布局的按钮的听众在代码!我可以在哪里写碎片的java代码?

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


     } 



    } 


} 

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

+0

把它们放到'安卓:onClick'布局XML。 – m0skit0

回答

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; 
} 
} 
相关问题