帮我生成一段安卓代码,运用动态fragment使用四个按钮来切换我的NAME(祝杭杰),AGE(21),STUDENT ID(2020329621145),和HOBBY(rap)
时间: 2024-02-05 15:04:58 浏览: 175
android实现点击按钮切换不同的fragment布局
以下是使用动态fragment切换四个不同的信息的示例代码:
MainActivity.java
```java
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnName, btnAge, btnId, btnHobby;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnName = findViewById(R.id.btn_name);
btnAge = findViewById(R.id.btn_age);
btnId = findViewById(R.id.btn_id);
btnHobby = findViewById(R.id.btn_hobby);
btnName.setOnClickListener(this);
btnAge.setOnClickListener(this);
btnId.setOnClickListener(this);
btnHobby.setOnClickListener(this);
// 默认显示姓名
replaceFragment(new NameFragment());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_name:
replaceFragment(new NameFragment());
break;
case R.id.btn_age:
replaceFragment(new AgeFragment());
break;
case R.id.btn_id:
replaceFragment(new IdFragment());
break;
case R.id.btn_hobby:
replaceFragment(new HobbyFragment());
break;
default:
break;
}
}
private void replaceFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
}
}
```
activity_main.xml
```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:layout_gravity="bottom|center_horizontal"
android:orientation="horizontal"
android:padding="16dp">
<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"
android:layout_marginStart="16dp" />
<Button
android:id="@+id/btn_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
android:layout_marginStart="16dp" />
<Button
android:id="@+id/btn_hobby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hobby"
android:layout_marginStart="16dp" />
</LinearLayout>
```
NameFragment.java
```java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class NameFragment extends Fragment {
private TextView tvInfo;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_info, container, false);
tvInfo = view.findViewById(R.id.tv_info);
tvInfo.setText("祝杭杰");
return view;
}
}
```
AgeFragment.java、IdFragment.java、HobbyFragment.java与NameFragment.java类似,只需要修改对应的信息即可。具体示例代码可以参考上面的代码进行修改。
阅读全文