gpt4 book ai didi

java - 如何打开和关闭fragment,点击android中的同一个按钮

转载 作者:行者123 更新时间:2023-12-01 16:20:51 54 4
gpt4 key购买 nike

如何在单击同一按钮时打开和关闭 fragment 。就像亚马逊的过滤按钮一样。当我们第一次单击时, fragment 应该出现,然后我们再次单击该按钮, fragment 应该消失。

业务 Activity java文件

public class business extends AppCompatActivity {

TextView t1, t2;
Button hello;
boolean flag = false;
FragmentTransaction fragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_business);
hello = findViewById(R.id.hello);
t1=findViewById(R.id.text1);
t2=findViewById(R.id.textt2);

}

@Override
protected void onStart() {
super.onStart();
hello.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
BusinessSort businessSort = new BusinessSort();
fragmentTransaction = manager.beginTransaction();
if (flag) {
fragmentTransaction.remove(businessSort);
flag=false;
} else {
fragmentTransaction.add(R.id.frgmCont, businessSort);
flag=true;
}
fragmentTransaction.commit();
}
});

}


public void f1(String s1, String s2) {
t1.setText(s1);
t2.setText(s2);
}

}

activity_business.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".business"
android:orientation="vertical"
android:id="@+id/businessLinearLayout">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/hello"
/>

<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="edit2"
android:id="@+id/textt2"/>

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

fragment 文件BusinessSort.java

public class BusinessSort extends Fragment {

EditText editText1,editText2;
Button submit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_business_sort, container, false);
editText1=view.findViewById(R.id.edit1);
editText2=view.findViewById(R.id.edit2);
submit=view.findViewById(R.id.businessfragment_submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1 = editText1.getText().toString();
String s2 = editText2.getText().toString();
business business = (com.example.project.business) getActivity();
business.f1(s1,s2);

}
});

return view;
}

fragment_business_sort.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BusinessSort"
android:orientation="vertical">

<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="50dp"/>
<Button
android:id="@+id/businessfragment_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"/>
</LinearLayout>

最佳答案

尝试替换 fragment 。

 fragmentTransaction.replace(R.id.frgmCont, businessSort);
flag = true;

我希望代码与我的方法基本相似,因此请告诉我问题是否仍然存在,以便我更深入地研究它。

关于java - 如何打开和关闭fragment,点击android中的同一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62286673/

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