gpt4 book ai didi

java - 安卓 fragment

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:52 31 4
gpt4 key购买 nike

我有一个问题,我的 Activity 上有三个按钮,当单击一个按钮时,我想打开一个特定的 fragment 。我该怎么做?这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);

bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (bt1.isClickable()){
Fragment_one f1 = new Fragment_one();
fragmentTransaction.add(R.id.fragment_container, f1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
});

最佳答案

尝试这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);

bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// use either changeFragment() or replaceFragment methods to switch between your fragments
Fragment_one f1 = new Fragment_one();
changeFragment(f1);
// replaceFragment(f1);
}
});

// this method will add a fragment if it not exist or show it if exists
public void changeFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();

String tag = fragment.getClass().getName();
// Check if fragment was added already
boolean alreadyAdded = fragmentManager.findFragmentByTag(tag) != null;

// hide all added fragments
List<Fragment> fragments = fragmentManager.getFragments();
if (fragments != null) {
for (Fragment f : fragments) {
transaction.hide(f);
}
}

// if the given fragment is added already just show it
// if not then add it
if (alreadyAdded) {
transaction.show(fragment);
} else {
transaction.add(R.id.fragment_container, fragment, tag);
// add to back stack will store just added fragments
transaction.addToBackStack(tag);
}
transaction.commit();
}

// this method will replace current fragment with the given one
public void replaceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
String tag = fragment.getClass().getName();
transaction.replace(R.id.fragment_container, fragment, tag);
transaction.addToBackStack(tag);
transaction.commit();
}

您不需要在 onClick() 中检查按钮是否可点击,因为如果不可点击,则根本不会调用 onClick()

关于java - 安卓 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36434723/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com