【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity_第1頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity_第2頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity_第3頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity_第4頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity

本文章向大家介紹怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,主要包括怎么在Android中實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity的使用實例、應(yīng)用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。Android——Fragment的靜態(tài)注冊和動態(tài)注冊為了實現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,下面需要創(chuàng)建以下文件:第一步:簡單編寫布局文件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實例

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并實現(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ù)。第三步:分別編寫fragment_one.xml和fragment_two.xml布局文件并通過編寫FragmentOne.java和FragmentTwo.java綁定對應(yīng)的布局文件,并實現(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="點擊下面的按鈕跳轉(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論