![](/img/trans.png)
[英]How to replace the content of one fragment with another in TabsLayout?
[英]How to replace one fragment with another with two buttons
我正在打开应用程序,但它崩溃了
你可以在按钮的onClick里面写FragmentTransaction 。 按照下面的代码,
public class MainActivity extends AppCompatActivity {
Button frag1, frag2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frag1 = (Button) findViewById(R.id.frag1btn);
frag2 = (Button) findViewById(R.id.frag2btn);
frag1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new Fragment1());
trans.commit();
}
});
frag2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
trans.replace(R.id.fragment_container, new Fragment2());
trans.commit();
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.