您好,登錄后才能下訂單哦!
一、activity
一般書寫代碼時在oncreate方法中引用initview()、initdata()、initload()進(jìn)行初始化
package com.ping.myfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends FragmentActivity {//當(dāng)activity中引入fragment時應(yīng)該繼承--FragmentActivity
private Myfragment myfragment;
private TextView activityTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initdata();
}
private void initdata() {
FragmentManager fm = getSupportFragmentManager();//獲得fragment的管理對象
FragmentTransaction ft = fm.beginTransaction();
myfragment = new Myfragment();//Fragment每次添加都要重新創(chuàng)建,否則因為狀態(tài)不同會導(dǎo)致問題
ft.add(R.id.container, myfragment);
ft.commit();
}
private void initview() {
activityTV = (TextView) findViewById(R.id.tv_activity);
findViewById(R.id.bt_activity).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myfragment.settext();
}
});
}
public void settext() {
activityTV.setText("activity修改成功");
}
}
二、fragment
package com.ping.myfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
public class Myfragment extends Fragment {
private TextView fragmentTV;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.layout_myfragment, null);
initview(inflate);
return inflate;
}
private void initview(View inflate) {
final MainActivity activity = (MainActivity) getActivity();
fragmentTV = (TextView) inflate.findViewById(R.id.tv_fragment);
inflate.findViewById(R.id.bt_fragment).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
activity.settext();
}
});
}
public void settext() {
fragmentTV.setText("fragment修改成功");
}
}
三activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</FrameLayout>
<TextView
android:id="@+id/tv_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/container"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="activity中的textview" />
<Button
android:id="@+id/bt_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_activity"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="改變fragment中的textview" />
</RelativeLayout>
四、fragmen.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee">
<TextView
android:id="@+id/tv_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="fragment中的textview" />
<Button
android:id="@+id/bt_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_fragment"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="改變activity中的textview" />
</RelativeLayout>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。