标签:
package com.example.testfragment; import com.example.testfragment.MainFargment.BackString; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends Activity implements BackString{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //静态加载 //Intent intent = new Intent(this,MainActivity2.class); //startActivity(intent); MainFargment fargment = new MainFargment(); //对Fragment通信传参数 Bundle bundle = new Bundle(); bundle.putString("name", "Michale"); fargment.setArguments(bundle); //动态加载 FragmentManager管理者 FragmentManager manager = getFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); //第一个参数指定目标 动态目标容器 transaction.add(R.id.linearLayout1, fargment, "fargment"); transaction.addToBackStack(null); transaction.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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public Void WriteString(String code) { // TODO 自动生成的方法存根 Toast.makeText(this, code, Toast.LENGTH_SHORT).show(); return null; } }
package com.example.testfragment; import android.app.Activity; import android.os.Bundle; public class MainActivity2 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); } }
package com.example.testfragment; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; public class MainFargment extends Fragment { private String name = "hello michale"; public BackString backString; public interface BackString { public Void WriteString(String code); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 自动生成的方法存根 //return super.onCreateView(inflater, container, savedInstanceState); View view=inflater.inflate(R.layout.userfragment, container, false); String strName = getArguments().getString("name").toString(); EditText text = (EditText) view.findViewById(R.id.editText1); text.setText(strName); backString.WriteString(name); return view; } @Override public void onAttach(Activity activity) { // TODO 自动生成的方法存根 backString = (BackString) activity; super.onAttach(activity); } }
<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.testfragment.MainActivity" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </RelativeLayout>
<?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:orientation="vertical" > <fragment android:id="@+id/fragment1" android:name="com.example.testfragment.MainFargment" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
<?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:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/zxcnn/p/5018941.html