public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String TAG = MainActivity.class.getSimpleName(); Button button1,button2,button3,button4,button5,button6; private ServiceConnection mServiceConnection; private DownloadBinder mDownloadBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); button1 = findViewById(R.id.button1); button2 = findViewById(R.id.button2); button3 = findViewById(R.id.button3); button4 = findViewById(R.id.button4); button5 = findViewById(R.id.button5); button6 = findViewById(R.id.button6); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); button4.setOnClickListener(this); button5.setOnClickListener(this); button6.setOnClickListener(this); mServiceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { mDownloadBinder=(DownloadBinder) iBinder; } @Override public void onServiceDisconnected(ComponentName componentName) { } }; } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button1: Intent startIntent = new Intent(this,MyService.class); startService(startIntent); break; case R.id.button2: Intent stopIntent = new Intent(this,MyService.class); stopService(stopIntent); break; case R.id.button3: Intent bindIntent = new Intent(this,MyService.class); bindService(bindIntent,mServiceConnection,BIND_AUTO_CREATE); break; case R.id.button4: unbindService(mServiceConnection); break; case R.id.button5: mDownloadBinder.startDownload(); break; case R.id.button6: mDownloadBinder.getProgress(); break; } } } public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private String TAG = MainActivity.class.getSimpleName(); ViewPager viewpager; RadioButton rb1,rb2,rb3,rb4,rb5,rb6,rb7,curRb; RadioGroup radios; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); rb1 = findViewById(R.id.rb1); rb2 = findViewById(R.id.rb2); rb3 = findViewById(R.id.rb3); rb4 = findViewById(R.id.rb4); rb5 = findViewById(R.id.rb5); rb6 = findViewById(R.id.rb6); rb7 = findViewById(R.id.rb7); radios = findViewById(R.id.radios); radios.setOnCheckedChangeListener(this); viewpager = findViewById(R.id.viewpager); List<Fragment> fragmentList = new ArrayList<>(); List<String> mTitleList = new ArrayList<>(); for (int i=0;i<7;i++) { fragmentList.add(new Fragment()); int x = i+1; mTitleList.add("第"+x+"个"); } MyFragmentPageAdapter fragmentPageAdapter = new MyFragmentPageAdapter(getSupportFragmentManager(),0,fragmentList,mTitleList); viewpager.setAdapter(fragmentPageAdapter); viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { Log.d(TAG, "onPageSelected: "+position); changeViewPager(position); } @Override public void onPageScrollStateChanged(int state) { } }); curRb =rb1; changeViewPager(0); } private void changeViewPager(Integer Id) { curRb.setChecked(false); viewpager.setCurrentItem(Id); switch (Id) { case 0: curRb = rb1; break; case 1: curRb = rb2; break; case 2: curRb = rb3; break; case 3: curRb = rb4; break; case 4: curRb = rb5; break; case 5: curRb = rb6; break; case 6: curRb = rb7; break; } curRb.setChecked(true); } @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i) { case R.id.rb1: if(rb1.isChecked()) { changeViewPager(0); } break; case R.id.rb2: if(rb2.isChecked()) { changeViewPager(1); } break; case R.id.rb3: if(rb3.isChecked()) { changeViewPager(2); } break; case R.id.rb4: if(rb4.isChecked()) { changeViewPager(3); } break; case R.id.rb5: if(rb5.isChecked()) { changeViewPager(4); } break; case R.id.rb6: if(rb6.isChecked()) { changeViewPager(5); } break; case R.id.rb7: if(rb7.isChecked()) { changeViewPager(6); } break; } } } public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String TAG = MainActivity.class.getSimpleName(); Button main_btn_1,main_btn_2,main_btn_3,main_btn_4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); main_btn_1 = findViewById(R.id.main_btn_1); main_btn_2 = findViewById(R.id.main_btn_2); main_btn_3 = findViewById(R.id.main_btn_3); main_btn_4 = findViewById(R.id.main_btn_4); main_btn_1.setOnClickListener(this); main_btn_2.setOnClickListener(this); main_btn_3.setOnClickListener(this); main_btn_4.setOnClickListener(this); } @Override public void onClick(View view) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); switch (view.getId()){ case R.id.main_btn_1: transaction.replace(R.id.main_fm,new Fragment1()); break; case R.id.main_btn_2: transaction.replace(R.id.main_fm,new Fragment2()); break; case R.id.main_btn_3: transaction.replace(R.id.main_fm,new Fragment3()); break; case R.id.main_btn_4: transaction.replace(R.id.main_fm,new Fragment4()); break; } transaction.commit(); } } public class MainActivity extends AppCompatActivity { FrameLayout fra_title; FrameLayout fra_content; private String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fra_title = findViewById(R.id.fra_title); fra_content = findViewById(R.id.fra_content); FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.add(R.id.fra_title,new TitleFragment()); transaction.add(R.id.fra_content,new ContentFragment()); transaction.commit(); } }