如何從fragment中點擊一個Button跳到另一個activity

        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);
}
}

發佈了21 篇原創文章 · 獲贊 10 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章