<android.support.v4.view.ViewPager android:id="@+id/mViewPager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/mRadioGroup" android:layout_marginBottom="50dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> </RadioGroup> <Button android:id="@+id/mStart" android:visibility="gone" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginBottom="50dp" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/mGo" android:text="跳过" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
package com.example.a; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ViewPager mViewPager; private RadioGroup mRadioGroup; private Button mStart; private Button mGo; private Adapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE); boolean tag = flag.getBoolean("tag", false); if (tag) { startActivity(new Intent(this, Main2Activity.class)); } initView(); initData(); } private List<String> mList = new ArrayList<>(); private void initData() { mList.add("http://p5.so.qhimgs1.com/t0101dc235116b51cdb.jpg"); mList.add("http://p5.so.qhimgs1.com/sdr/200_200_/t014551ed7ace33fd4b.jpg"); mList.add("http://p4.so.qhmsg.com/t01c099eede38cc1b99.jpg"); for (int i = 0; i < mList.size(); i++) { RadioButton button = new RadioButton(this); button.setId(i); button.setButtonDrawable(R.drawable.radiobutton_selector); if (i == 0) { button.setChecked(true); } mRadioGroup.addView(button); adapter.notifyDataSetChanged(); } } private void initView() { mViewPager = (ViewPager) findViewById(R.id.mViewPager); mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup); mStart = (Button) findViewById(R.id.mStart); mGo = (Button) findViewById(R.id.mGo); // 适配器 adapter = new Adapter(MainActivity.this, mList); mViewPager.setAdapter(adapter); mStart.setOnClickListener(this); mGo.setOnClickListener(this); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { ((RadioButton) mRadioGroup.getChildAt(position)).setChecked(true); if (mList.size() - 1 == position) { mRadioGroup.setVisibility(View.GONE); mStart.setVisibility(View.VISIBLE); } else { mRadioGroup.setVisibility(View.VISIBLE); mStart.setVisibility(View.GONE); } } @Override public void onPageScrollStateChanged(int state) { } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.mStart: case R.id.mGo: SharedPreferences flag = getSharedPreferences("flag", MODE_PRIVATE); SharedPreferences.Editor edit = flag.edit(); edit.putBoolean("tag", true); edit.commit(); startActivity(new Intent(MainActivity.this, Main2Activity.class)); break; } } }
//radiobutton_selector.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/selected"/> <item android:state_checked="false" android:drawable="@drawable/unselected"/> </selector>//selected.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <solid android:color="@color/colorPrimaryDark"/> </shape>//unselected.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <solid android:color="#eee5de"/> </shape>