Fragment 代码怎么写

时间:2024-09-21 21:37:08
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()