Fragment跳转

### activity_main布局

<FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

###login布局


<RelativeLayout 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/et_name"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView1"
        android:hint="输入姓名" >
    </EditText>

    <TextView
        android:id="@+id/textView_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_name"
        android:layout_marginTop="57dp"
        android:text="密码" />

    <EditText
        android:id="@+id/et_pass"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView_pass"
        android:layout_alignBottom="@+id/textView_pass"
        android:layout_toRightOf="@+id/textView_pass"
        android:hint="输入密码" >
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/et_name"
        android:layout_alignBottom="@+id/et_name"
        android:layout_toLeftOf="@+id/et_pass"
        android:text="姓名" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_pass"
        android:layout_centerVertical="true"
        android:layout_marginLeft="44dp"
        android:text="跳转" />

</RelativeLayout>


###home布局

 <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="首页"
        android:textSize="50sp" />


###MainActivity

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.fl, new Fragment1(), "f1").commit();
    }


   
   
}

###Fragment1

public class Fragment1 extends Fragment {

 private View view;

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {

  view = View.inflate(getActivity(), R.layout.login, null);

  return view;
 }
 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  
  FragmentManager fm=getActivity().getSupportFragmentManager();
  final FragmentTransaction ft=fm.beginTransaction();
  
  
  Button button=(Button) getView().findViewById(R.id.button);
  final EditText tv_name=(EditText) getView().findViewById(R.id.et_name);
  final EditText tv_pass=(EditText) getView().findViewById(R.id.et_pass);
  
  button.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    String name=tv_name.getText().toString();
    String pass=tv_pass.getText().toString();
    if(TextUtils.isEmpty(name)){
     Toast.makeText(getActivity(), "用户名不能为空", 0).show();
     return ;
    }else if(TextUtils.isEmpty(pass)){
     Toast.makeText(getActivity(), "密码不能为空", 0).show();
     return ;
    }
    //跳转fragment
    Fragment2 f2=new Fragment2();
    Bundle bundle=new Bundle();
    bundle.putString("name", name);
    bundle.putString("pass", pass);
    f2.setArguments(bundle);
    //把fragment2加入到回退栈
    ft.replace(R.id.fl, f2);
    ft.addToBackStack(null);
    ft.commit();
    
   }
  });
  
  
  
  super.onActivityCreated(savedInstanceState);
 }
 
 
}


###Fragment2

public class Fragment2 extends Fragment {

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  
  View view=View.inflate(getActivity(), R.layout.home, null);
  
  
  return view;
  
 }
 @SuppressLint("NewApi") @Override
 public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  Bundle bundle=getArguments();
  String name=bundle.getString("name", "");
  String pass=bundle.getString("pass", "");
  
  TextView tv=(TextView) getView().findViewById(R.id.tv);
  tv.setText("姓名:"+name+"\n密码:"+pass);
  
 }
}














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值