Fragment 代码怎么写

简介: public class Voice extends Fragment implements OnClickListener { public View onCreateView(LayoutInflater inflater, ViewGroup container, ...
public class Voice extends Fragment implements OnClickListener {
 

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
         System.out.println("ExampleFragment--onCreateView");
            View view = inflater.inflate(R.layout.voice, container, false);
            
             view.findViewById(R.id.button1).setOnClickListener(this);
               view.findViewById(R.id.button2).setOnClickListener(this);
            
               return view;
                 
     }
     
     //http://www.cnblogs.com/xiaobo-Linux/
     
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
     //     initLayout();
         System.out.println("oncreate");
    }  

     

     
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.button1:
         
       getActivity().startService(new Intent(getActivity(),VoiceService.class));     
            break;
         
        case R.id.button2:
       
       getActivity().stopService(new Intent(getActivity(),VoiceService.class));        
      
            break;
     
        default:
            break;
        }
    }

    
public void onPause()
{
     super.onPause();
     System.out.println("ExampleFragment--onPause");
     
}

     
    
}

 

 

在 oncreateView 里面 进行页面初始化

view view.findViewById

this 改成 getActivity()

目录
相关文章
|
前端开发
前端学习案例3-fragment之3
前端学习案例3-fragment之3
49 0
前端学习案例3-fragment之3
|
前端开发
前端学习案例2-fragment之2
前端学习案例2-fragment之2
50 0
前端学习案例2-fragment之2
|
前端开发
前端学习案例2-_fragment
前端学习案例2-_fragment
43 0
前端学习案例2-_fragment
|
前端开发
前端学习案例1-_fragment
前端学习案例1-_fragment
51 0
前端学习案例1-_fragment
|
前端开发
前端学习案例3-_fragment
前端学习案例3-_fragment
56 0
前端学习案例3-_fragment
|
前端开发
前端学习案例1-fragment之1
前端学习案例1-fragment之1
55 0
前端学习案例1-fragment之1
|
Android开发
图+源码,读懂View的Layout方法
本篇文章就带大家学习 View 绘制三大方法的第二个方法——Layout 方法。
图+源码,读懂View的Layout方法
单Acitivty+Fragment的优化写法。
一直以来,在写单Acitivty+Fragment的时候,标题栏的布局虽然 include 同一个,但是每次代码里都要对标题栏进行setText() ,而且每次都要写一遍标题栏的点击事件,而在以前的学习使用中,都没有考虑过去对它们进行一个优化。
65 0