main
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="联系人"
/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="聊天"
android:layout_weight="1" />
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
f1 xml
<?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/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/upwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:password="true"
android:singleLine="true" />
<Button
android:id="@+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
f2xml
<?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" >
<TextView
android:layout_gravity="center"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="TextView" />
<Button
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back" />
</LinearLayout>
public class F1 extends Fragment implements OnClickListener{
private View view;
private Button bt;
private EditText uname;
private EditText pwd;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f1,container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
bt = (Button)view.findViewById(R.id.save);
uname=(EditText)view.findViewById(R.id.uname);
pwd=(EditText)view.findViewById(R.id.upwd);
//给保存按钮设置监听
bt.setOnClickListener(this);
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
F2 f2=new F2();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.replace(R.id.fl,f2).addToBackStack(null);
beginTransaction.commit();
String g_name=uname.getText().toString();
String g_pwd=pwd.getText().toString();
Bundle args=new Bundle();
args.putString("name",g_name);
args.putString("pwd",g_pwd);
f2.setArguments(args);
}
}
public class F2 extends Fragment implements OnClickListener{
private View view;
private TextView tv;
private Button back;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.f2,container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
tv = (TextView) view.findViewById(R.id.textView2);
back=(Button)view.findViewById(R.id.back);
back.setOnClickListener(this);
Bundle agrs = getArguments();
tv.setText(agrs.getString("name")+"----"+agrs.getString("pwd"));
super.onActivityCreated(savedInstanceState);
}
@Override
public void onClick(View v) {
F1 f1=new F1();
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction();
beginTransaction.replace(R.id.fl, f1);
beginTransaction.commit();
}
}
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建对象
F1 f1=new F1();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction();
beginTransaction.add(R.id.fl,f1);
beginTransaction.commit();
}
}