码迷,mamicode.com
首页 > 其他好文 > 详细

Fragment 代码怎么写

时间:2016-05-01 20:28:22      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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()

Fragment 代码怎么写

标签:

原文地址:http://www.cnblogs.com/xiaobo-Linux/p/5451009.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!