您好,登錄后才能下訂單哦!
一、activity
一般書寫代碼時在oncreate方法中引用initview()、initdata()、initload()進行初始化
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 {//當activity中引入fragment時應該繼承--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每次添加都要重新創建,否則因為狀態不同會導致問題
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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。