Android有一個新特性,增加了fragment。但是在fragment中沒有了setContentView,也不能直接通過findViewById使得Button和佈局文件中的按鈕聯繫起來。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_1, null);
Button PresentLoc = (Button) view.findViewById(R.id.button1); //此處使得Button和xml中的按鈕聯繫
PresentLoc.setOnClickListener(new LocationCheckedListener()); //這一行是在將button和監聽器捆綁
return view;
}
class LocationCheckedListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getActivity(), LocationPage.class); //從前者跳到後者,特別注意的是,在fragment中,用getActivity()來獲取當前的activity
getActivity().startActivity(intent);
}
}