



下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity
本文章向大家介紹怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,主要包括怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity的使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。Android是一種基于Linux內(nèi)核的自由及開(kāi)放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開(kāi)放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開(kāi)發(fā)。Android——Fragment的靜態(tài)注冊(cè)和動(dòng)態(tài)注冊(cè)為了實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,下面需要?jiǎng)?chuàng)建以下文件:第一步:簡(jiǎn)單編寫(xiě)布局文件fragment_activity.xml和抽象類TemplateFragmentActivity.java代碼如下:fragment_activity.xml<?xml
version="1.0"
encoding="utf-8"?>
<FrameLayout
xmlns:android="/apk/res/android"
android:id="@+id/temp_fragment_activity"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>fragment_activity.xml布局主要用于承載各fragment布局,例如fragment_one.xml和fragment_two.xml。TemplateFragmentActivity.javapackage
com.example.myapplication;
import
android.os.Bundle;
import
androidx.annotation.Nullable;
import
androidx.appcompat.app.AppCompatActivity;
import
androidx.fragment.app.Fragment;
import
androidx.fragment.app.FragmentManager;
import
androidx.fragment.app.FragmentTransaction;
public
abstract
class
TemplateFragmentActivity
extends
AppCompatActivity
{
private
FragmentManager
fm;
private
FragmentTransaction
ts;
private
Fragment
fragment;
//抽象方法,用于創(chuàng)建Fragment實(shí)例
protected
abstract
Fragment
createFragment();
@Override
protected
void
onCreate(@Nullable
Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_activity);
fm
=
getSupportFragmentManager();
ts
=
fm.beginTransaction();
if
(fragment
==
null){
fragment
=
createFragment();
ts.add(R.id.temp_fragment_activity,fragment);
mit();
}
}
}第二步:分別使類FragmentOneActivity和FragmentTwoActivity繼承類TemplateFragmentActivity并實(shí)現(xiàn)抽象方法createFragment()FragmentOneActivity.javapackage
com.example.myapplication;
import
androidx.fragment.app.Fragment;
public
class
FragmentOneActivity
extends
TemplateFragmentActivity
{
@Override
protected
Fragment
createFragment()
{
return
new
FragmentOne();
}
}FragmentTwoActivity.java與FragmentOneActivity.java類似,不在重復(fù)。第三步:分別編寫(xiě)fragment_one.xml和fragment_two.xml布局文件并通過(guò)編寫(xiě)FragmentOne.java和FragmentTwo.java綁定對(duì)應(yīng)的布局文件,并實(shí)現(xiàn)其具體功能。fragment_one.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="點(diǎn)擊下面的按鈕跳轉(zhuǎn)到FragmentTwoActivity"
android:textSize="20sp"
android:textAllCaps="false"
android:textColor="#F70505">
</TextView>
<Button
android:id="@+id/btn_fm_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳轉(zhuǎn)"
android:textSize="30dp"
android:layout_marginTop="20dp">
</Button>
</LinearLayout>fragment_two.xml與fragment_one.xml類似,不在重復(fù)。FragmentOne.javapackage
com.example.myapplication;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.Button;
import
androidx.annotation.NonNull;
import
androidx.annotation.Nullable;
import
androidx.fragment.app.Fragment;
public
class
FragmentOne
extends
Fragment
{
private
Button
mBtnFragmentOne;
@Nullable
@Override
public
View
onCreateView(@NonNull
LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
View
view
=
inflater.inflate(R.layout.fragment_one,container,false);
mBtnFragmentOne
=
view.findViewById(R.id.btn_fm_one);
mBtnFragmentOne.setOnClickListener(new
View.OnClickListener()
{
@Override
public
void
onClick(View
v)
{
Intent
intent
=
new
Intent(getActivity(),FragmentTwoActivity.class);
startActivity(intent);
}
}
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)生評(píng)教與反饋實(shí)施方案計(jì)劃
- 靜脈治療報(bào)告
- 統(tǒng)編版小學(xué)語(yǔ)文二年級(jí)下冊(cè)《語(yǔ)文園地三》精美課件
- 第四單元 《平行四邊形的認(rèn)識(shí)》教學(xué)設(shè)計(jì)-2024-2025學(xué)年四年級(jí)數(shù)學(xué)上冊(cè)青島版(五四學(xué)制)
- 養(yǎng)老床位建設(shè)服務(wù)方案(技術(shù)方案)
- 老年骨折手術(shù)護(hù)理
- 放射科護(hù)理相關(guān)知識(shí)課件
- 培訓(xùn)課件知識(shí)產(chǎn)權(quán)保護(hù)
- 2025年湛江道路客貨運(yùn)輸從業(yè)資格證模擬考試下載
- 2025年上海貨運(yùn)從業(yè)資格證模擬試題答案大全
- 語(yǔ)文-湖南省長(zhǎng)郡二十校聯(lián)盟2025屆新高考教學(xué)教研聯(lián)盟高三第一次聯(lián)考(長(zhǎng)郡二十校一聯(lián))試題和答案
- 金蝶云星空操作手冊(cè)V3
- 醫(yī)療衛(wèi)生中心社會(huì)效益與經(jīng)濟(jì)效益分析
- 3月3號(hào)全國(guó)愛(ài)耳日-保護(hù)耳朵課件
- 2025年遼寧裝備制造職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)(網(wǎng)校專用)
- 2025國(guó)家電投集團(tuán)資本控股限公司本部招聘11人高頻重點(diǎn)模擬試卷提升(共500題附帶答案詳解)
- 安全生產(chǎn)風(fēng)險(xiǎn)防控“六項(xiàng)機(jī)制”做法及經(jīng)驗(yàn)分享
- 2025年湖南中醫(yī)藥高等??茖W(xué)校高職單招職業(yè)技能測(cè)試近5年??及鎱⒖碱}庫(kù)含答案解析
- 2024新版人教PEP英語(yǔ)(2025春)七年級(jí)下冊(cè)教學(xué)課件:Unit2 Reading Plus
- 《小兔子安家》(說(shuō)課稿)-2024-2025學(xué)年一年級(jí)下冊(cè)數(shù)學(xué)北師大版
- 小學(xué)生人際交往能力培養(yǎng)的實(shí)踐研究
評(píng)論
0/150
提交評(píng)論