安卓Day04

安卓Day04

#fragment
##fragment的意义:是切换页面
#fragment的用法
主类的写法

ckage com.example.myday004;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=findViewById(R.id.one_edit);
        button=findViewById(R.id.one_btn);

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.oen_flt,new TwoFragment());
        fragmentTransaction.commit();



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

                TwoFragment twoFragment = new TwoFragment();
                Bundle bundle = new Bundle();
                //发送的信息
                bundle.putString("name",editText.getText().toString());
                //存放bundle对象
                twoFragment.setArguments(bundle);

                fragmentTransaction.replace(R.id.oen_flt,twoFragment);
                fragmentTransaction.commit();


            }
        });


    }
}

fragment类的写法

ckage com.example.myday004;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText=findViewById(R.id.one_edit);
        button=findViewById(R.id.one_btn);

        FragmentManager supportFragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.oen_flt,new TwoFragment());
        fragmentTransaction.commit();



        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();

                TwoFragment twoFragment = new TwoFragment();
                Bundle bundle = new Bundle();
                //发送的信息
                bundle.putString("name",editText.getText().toString());
                //存放bundle对象
                twoFragment.setArguments(bundle);

                fragmentTransaction.replace(R.id.oen_flt,twoFragment);
                fragmentTransaction.commit();


            }
        });


    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值