本文章向大家介紹怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity,主要包括怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity的使用實(shí)例、應(yīng)用技巧、基本知識點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、彭水苗族土家族網(wǎng)站維護(hù)、網(wǎng)站推廣。
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
Android——Fragment的靜態(tài)注冊和動態(tài)注冊
為了實(shí)現(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="http://schemas.android.com/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.java
package 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);
ts.commit();
}
}
}第二步:分別使類FragmentOneActivity和FragmentTwoActivity繼承類TemplateFragmentActivity并實(shí)現(xiàn)抽象方法createFragment()
FragmentOneActivity.java
package 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)的布局文件,并實(shí)現(xiàn)其具體功能。
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/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.java
package 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);
}
});
return view;
}
}Fragment跳轉(zhuǎn)到Activity與Activity跳轉(zhuǎn)到Activity方法類似,如下:
Intent intent = new Intent(getActivity(),FragmentTwoActivity.class); startActivity(intent);
FragmentTwo.java與FragmentOne .java類似,不在重復(fù)。
到此這篇關(guān)于怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity的文章就介紹到這了,更多相關(guān)的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!
網(wǎng)頁標(biāo)題:怎么在Android中實(shí)現(xiàn)從Fragment跳轉(zhuǎn)到其他Activity
URL分享:http://chinadenli.net/article16/goiogg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、商城網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)