帮我生成一段完整Android代码,运用动态fragment使用四个按钮来切换我的NAME(祝杭杰),AGE(21),STUDENT ID(2020329621145),和HOBBY(rap)
时间: 2024-02-05 09:02:32 浏览: 28
以下是可以实现您所说功能的完整Android代码:
MainActivity.java
```
import android.os.Bundle;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private NameFragment nameFragment;
private AgeFragment ageFragment;
private StudentIdFragment studentIdFragment;
private HobbyFragment hobbyFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
// 初始化四个Fragment
nameFragment = new NameFragment();
ageFragment = new AgeFragment();
studentIdFragment = new StudentIdFragment();
hobbyFragment = new HobbyFragment();
// 设置四个按钮的点击事件
findViewById(R.id.btn_name).setOnClickListener(this);
findViewById(R.id.btn_age).setOnClickListener(this);
findViewById(R.id.btn_student_id).setOnClickListener(this);
findViewById(R.id.btn_hobby).setOnClickListener(this);
// 默认显示NameFragment
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, nameFragment).commit();
}
@Override
public void onClick(View v) {
fragmentTransaction = fragmentManager.beginTransaction();
switch (v.getId()) {
case R.id.btn_name:
fragmentTransaction.replace(R.id.fragment_container, nameFragment).commit();
break;
case R.id.btn_age:
fragmentTransaction.replace(R.id.fragment_container, ageFragment).commit();
break;
case R.id.btn_student_id:
fragmentTransaction.replace(R.id.fragment_container, studentIdFragment).commit();
break;
case R.id.btn_hobby:
fragmentTransaction.replace(R.id.fragment_container, hobbyFragment).commit();
break;
}
}
}
```
NameFragment.java
```
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class NameFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_name, container, false);
return view;
}
}
```
AgeFragment.java
```
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class AgeFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_age, container, false);
return view;
}
}
```
StudentIdFragment.java
```
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class StudentIdFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_student_id, container, false);
return view;
}
}
```
HobbyFragment.java
```
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class HobbyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hobby, container, false);
return view;
}
}
```
activity_main.xml
```
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/btn_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<Button
android:id="@+id/btn_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<Button
android:id="@+id/btn_student_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student ID" />
<Button
android:id="@+id/btn_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobby" />
</LinearLayout>
```
fragment_name.xml
```
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="祝杭杰" />
```
fragment_age.xml
```
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21" />
```
fragment_student_id.xml
```
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2020329621145" />
```
fragment_hobby.xml
```
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rap" />
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)