程序包含一个继承Activity的主类,另外两个继承Fragment类,并且两个Fragment共用一个xml layout文件
主类程序如下:
package com.example.androidfragmentd01test;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;@SuppressLint("NewApi")public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm=getFragmentManager(); addShowHideListener(R.id.frag1hide,fm.findFragmentById(R.id.fragment1)); addShowHideListener(R.id.frag2hide,fm.findFragmentById(R.id.fragment2)); } private void addShowHideListener(int button1, final Fragment findFragmentById) { // TODO Auto-generated method stub final Button button; button=(Button)findViewById(button1); button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub FragmentTransaction ft=getFragmentManager().beginTransaction(); ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if(findFragmentById.isHidden()){ ft.show(findFragmentById); button.setText("Hide"); }else{ ft.hide(findFragmentById); button.setText("Show"); } ft.commit(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
主类的相关配置文件:
新建Fragmentone和Fragmenttwo:
Fragmentone如下:package com.example.androidfragmentd01test;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;@SuppressLint("NewApi")public class FirstFragment extends Fragment{ private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The fragment saves and restores this text."); mTextView = (TextView)v.findViewById(R.id.saved); if (savedInstanceState != null) { mTextView.setText(savedInstanceState.getCharSequence("text")); } return v; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putCharSequence("text", mTextView.getText()); } }
Fragmenttwo如下:
package com.example.androidfragmentd01test;import android.annotation.SuppressLint;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;@SuppressLint("NewApi")public class SecondFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.text_edit, container, false); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The TextView saves and restores this text."); //另外一种TextView的保存模式 ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true); return v; } }
xml文件如下:
欲行即可.
<Linker from : >