package com.example.goukumusic; import android.annotation.SuppressLint; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; public class LoginSuccess extends AppCompatActivity implements View.OnClickListener{ // 声明Fragment对象 private Fragment_song fragment1; private Fragment_singer fragment2; private Fragment_audio fragment3; private Fragment_singleinformation fragment4; private Fragment nowFragment; // 声明底部标签 private TextView tab1,tab2,tab3,tab4; private ImageView image1,image2,image3,image4; private LinearLayout song; private LinearLayout singer; private LinearLayout audio; private LinearLayout information; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_success); initUI(); //初始化UI方法 } // 初始化UI界面 private void initUI(){ // 初始化底部图像 image1 = findViewById(R.id.imageone); image2 = findViewById(R.id.imagetwo); image3 = findViewById(R.id.imagethree); image4 = findViewById(R.id.imagefour); // 初始化底部标签 tab1 = findViewById(R.id.one); tab2 = findViewById(R.id.two); tab3 = findViewById(R.id.three); tab4 = findViewById(R.id.four); // 初始化布局标签 song = findViewById(R.id.song); singer = findViewById(R.id.singer); audio = findViewById(R.id.audio); information = findViewById(R.id.singleinformation); // 设置底部标签的变化,默认第一个被选中 image1.setImageResource(R.drawable.song1); tab1.setTextColor(Color.parseColor("#41C9E2")); tab2.setTextColor(Color.parseColor("#8a8a8a")); tab3.setTextColor(Color.parseColor("#8a8a8a")); tab4.setTextColor(Color.parseColor("#8a8a8a")); // 为底部标签设置点击事件 song.setOnClickListener(this); singer.setOnClickListener(this); audio.setOnClickListener(this); information.setOnClickListener(this); // 展示初始界面 showFragment1(); } // 设置Fragment1 private void showFragment1(){ // 开启事务,Fragment的切换是由事务控制 @SuppressLint("CommitTransaction") FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // 隐藏所有的Fragment hideAllFragment(transaction); // 判断Fragment是否为空 if(fragment1 == null){ fragment1 = new Fragment_song(); // 添加到Fragment1到事务中 transaction.add(R.id.content_layout,fragment1); } else { transaction.show(fragment1); } // // 记录fragment // nowFragment = fragment1; //提交事务 transaction.commit(); // 设置底部标签变化 tab1.setTextColor(Color.parseColor("#41C9E2")); tab2.setTextColor(Color.parseColor("#8a8a8a")); tab3.setTextColor(Color.parseColor("#8a8a8a")); tab4.setTextColor(Color.parseColor("#8a8a8a")); // 设置图像的变化 image1.setImageResource(R.drawable.song1); image2.setImageResource(R.drawable.singer); image3.setImageResource(R.drawable.audio); image4.setImageResource(R.drawable.singleinformation); } // 设置Fragment2 private void showFragment2(){ // 开启事务,Fragment的切换是由事务控制 @SuppressLint("CommitTransaction") FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // 隐藏所有的Fragment hideAllFragment(transaction); // 判断Fragment是否为空 if(fragment2 == null){ fragment2 = new Fragment_singer(); // 添加到Fragment2到事务中 transaction.add(R.id.content_layout,fragment2); } else{ // 显示Fragment transaction.show(fragment2); } // // 记录fragment // nowFragment = fragment2; //提交事务 transaction.commit(); // 设置底部标签变化 tab1.setTextColor(Color.parseColor("#8a8a8a")); tab2.setTextColor(Color.parseColor("#41C9E2")); tab3.setTextColor(Color.parseColor("#8a8a8a")); tab4.setTextColor(Color.parseColor("#8a8a8a")); // 设置图像的变化 image1.setImageResource(R.drawable.song); image2.setImageResource(R.drawable.singer1); image3.setImageResource(R.drawable.audio); image4.setImageResource(R.drawable.singleinformation); } // 设置Fragment3 private void showFragment3(){ // 开启事务,Fragment的切换是由事务控制 @SuppressLint("CommitTransaction") FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // 隐藏所有的Fragment hideAllFragment(transaction); // 判断Fragment是否为空 if(fragment3 == null){ fragment3 = new Fragment_audio(); // 添加Fragment3到事务中 transaction.add(R.id.content_layout,fragment3); } else{ // 显示Fragment transaction.show(fragment3); } // // 记录fragment // nowFragment = fragment3; //提交事务 transaction.commit(); // 设置底部标签变化 tab1.setTextColor(Color.parseColor("#8a8a8a")); tab2.setTextColor(Color.parseColor("#8a8a8a")); tab3.setTextColor(Color.parseColor("#41C9E2")); tab4.setTextColor(Color.parseColor("#8a8a8a")); // 设置图像的变化 image1.setImageResource(R.drawable.song); image2.setImageResource(R.drawable.singer); image3.setImageResource(R.drawable.audio1); image4.setImageResource(R.drawable.singleinformation); } // 设置Fragment4 private void showFragment4(){ // 开启事务,Fragment的切换是由事务控制 @SuppressLint("CommitTransaction") FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // 隐藏所有的Fragment hideAllFragment(transaction); // 判断Fragment是否为空 if(fragment4 == null){ fragment4 = new Fragment_singleinformation(); // 添加Fragment4到事务中 transaction.add(R.id.content_layout,fragment4); } else { // 显示Fragment transaction.show(fragment4); } // // 记录fragment // nowFragment = fragment4; //提交事务 transaction.commit(); // 设置底部标签变化 tab1.setTextColor(Color.parseColor("#8a8a8a")); tab2.setTextColor(Color.parseColor("#8a8a8a")); tab3.setTextColor(Color.parseColor("#8a8a8a")); tab4.setTextColor(Color.parseColor("#41C9E2")); // 设置图像的变化 image1.setImageResource(R.drawable.song); image2.setImageResource(R.drawable.singer); image3.setImageResource(R.drawable.audio); image4.setImageResource(R.drawable.singleinformation1); } // 隐藏所有的Fragment private void hideAllFragment(FragmentTransaction transaction){ if (fragment1 != null){ transaction.hide(fragment1); } if (fragment2 != null){ transaction.hide(fragment2); } if (fragment3 != null){ transaction.hide(fragment3); } if (fragment4 != null){ transaction.hide(fragment4); } } // 设置点击事件 @Override public void onClick(View view){ int id = view.getId(); if (id == R.id.song){ showFragment1(); } if (id == R.id.singer){ showFragment2(); } if (id == R.id.audio){ showFragment3(); } if (id == R.id.singleinformation){ showFragment4(); } } }