MyApp/java
package com.example.t072401;import android.app.Application;
public class MyApp extends Application {
public static String value1 = "";
}
SecondFragment extends Fragment / java
package com.example.t072401;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
public SecondFragment(){
Log.d("Fragment", "Fragment 2 constructor");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sec, container,
false);
Log.d("Fragment", "Fragment 2 rootView Created!");
return rootView;
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (this.getView() != null)
{
TextView tv = (TextView) this.getView().findViewById(R.id.textView1);
tv.setText(MyApp.value1);
Log.d("Fragment", "setMenuVisibility");
}
}
}
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
public SecondFragment(){
Log.d("Fragment", "Fragment 2 constructor");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sec, container,
false);
Log.d("Fragment", "Fragment 2 rootView Created!");
return rootView;
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (this.getView() != null)
{
TextView tv = (TextView) this.getView().findViewById(R.id.textView1);
tv.setText(MyApp.value1);
Log.d("Fragment", "setMenuVisibility");
}
}
}
MainActivity/java
package com.example.t072401;import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
public SecondFragment(){
Log.d("Fragment", "Fragment 2 constructor");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sec, container,
false);
Log.d("Fragment", "Fragment 2 rootView Created!");
return rootView;
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (this.getView() != null)
{
TextView tv = (TextView) this.getView().findViewById(R.id.textView1);
tv.setText(MyApp.value1);
Log.d("Fragment", "setMenuVisibility");
}
}
}
fragment_main
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.t072401.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/section_label"
android:layout_below="@+id/section_label"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
fragment_sec
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
沒有留言:
張貼留言