1. 程式人生 > >為Fragment內的按鈕新增監聽並跳轉到另一個Activity

為Fragment內的按鈕新增監聽並跳轉到另一個Activity

public class TaskListActivity extends Fragment implements OnClickListener {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_task_list, container,false);
    }

//新增onActivityCreated方法
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
Button button = (Button) getActivity().findViewById(R.id.cretask_btn);
button.setOnClickListener(this);

}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.cretask_btn:
        Intent intent = new Intent();
        intent.setClass(getActivity(), CreTaskActivity.class);
        startActivity(intent);
        break;
    }
}

簡單的跳轉,博主能力有限,勿噴。