安卓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();
}
});
}
}