首次进入 代码
package com.bwei.main;
import com.bwei.uu.R;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
public class MainActivity extends Activity {
private SharedPreferences sp;
private Handler hand = new Handler() {
public void handleMessage(android.os.Message msg) {
intents();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
new Thread() {
public void run() {
hand.sendEmptyMessageDelayed(0, 3000);
};
}.start();
}
private void intents() {
// SharedPreferences存储
sp = getSharedPreferences("config", MODE_PRIVATE);
if (sp.getBoolean("isFirst", false)) {
// 第二次进入跳转
Intent intent = new Intent(MainActivity.this, ThirdActivity.class);
startActivity(intent);
finish();
} else {
// 第一次进入跳转
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
sp.edit().putBoolean("isFirst", true).commit();
}
}
}
引导页 代码
package com.bwei.main;
import com.bwei.uu.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class SecondActivity extends Activity {
private ViewPager vp;
int[] ids=new int[]{R.drawable.vp1,R.drawable.vp2,R.drawable.vp3,R.drawable.vp4,R.drawable.vp5};
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_second);
//控件
vp = (ViewPager)findViewById(R.id.vp);
button1 = (Button)findViewById(R.id.button1);
//适配器
vp.setAdapter(new MyAdapter());
//监听事件
vp.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
if(arg0==ids.length-1){
button1.setVisibility(button1.VISIBLE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
public void btn(View v){
//跳转
Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
//关闭
finish();
}
//适配器
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return ids.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView iv=new ImageView(SecondActivity.this);
ScaleType scaleType=ScaleType.FIT_XY;
iv.setScaleType(scaleType);
iv.setImageResource(ids[position]);
container.addView(iv);
return iv;
}
}
}
主页加载 Fragment 布局代码
package com.bwei.main;
import com.bwei.fragment.FragMent1;
import com.bwei.fragment.FragMent2;
import com.bwei.fragment.FragMent3;
import com.bwei.fragment.FragMent4;
import com.bwei.fragment.FragMent5;
import com.bwei.uu.R;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class ThirdActivity extends FragmentActivity {
private RadioGroup rg1;
private RadioButton radio1;
private RadioButton radio2;
private RadioButton radio3;
private RadioButton radio4;
private RadioButton radio5;
private FragmentManager manager;
private FragMent1 f1;
private FragMent2 f2;
private FragMent3 f3;
private FragMent4 f4;
private FragMent5 f5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_third);
find();
initData();
}
private void initData() {
manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
f1 = new FragMent1();
f2 = new FragMent2();
f3 = new FragMent3();
f4 = new FragMent4();
f5 = new FragMent5();
transaction.add(R.id.fl, f1, "f1");
transaction.add(R.id.fl, f2, "f2");
transaction.add(R.id.fl, f3, "f3");
transaction.add(R.id.fl, f4, "f4");
transaction.add(R.id.fl, f5, "f5");
transaction.commit();
setfragment("f1");
radio1.setTextColor(Color.BLACK);
radioGroupListener();
}
private void radioGroupListener() {
// TODO Auto-generated method stub
rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.radio1:
radio1.setTextColor(Color.BLACK);
radio2.setTextColor(Color.WHITE);
radio3.setTextColor(Color.WHITE);
radio4.setTextColor(Color.WHITE);
radio5.setTextColor(Color.WHITE);
setfragment("f1");
break;
case R.id.radio2:
radio2.setTextColor(Color.BLACK);
radio1.setTextColor(Color.WHITE);
radio3.setTextColor(Color.WHITE);
radio4.setTextColor(Color.WHITE);
radio5.setTextColor(Color.WHITE);
setfragment("f2");
break;
case R.id.radio3:
radio3.setTextColor(Color.BLACK);
radio2.setTextColor(Color.WHITE);
radio1.setTextColor(Color.WHITE);
radio4.setTextColor(Color.WHITE);
radio5.setTextColor(Color.WHITE);
setfragment("f3");
break;
case R.id.radio4:
radio4.setTextColor(Color.BLACK);
radio2.setTextColor(Color.WHITE);
radio3.setTextColor(Color.WHITE);
radio1.setTextColor(Color.WHITE);
radio5.setTextColor(Color.WHITE);
setfragment("f4");
break;
case R.id.radio5:
radio5.setTextColor(Color.BLACK);
radio2.setTextColor(Color.WHITE);
radio3.setTextColor(Color.WHITE);
radio4.setTextColor(Color.WHITE);
radio1.setTextColor(Color.WHITE);
setfragment("f5");
break;
}
}
});
}
private void setfragment(String str) {
FragmentTransaction transaction = manager.beginTransaction();
if("f1".equals(str)){
transaction.show(f1);
transaction.hide(f2);
transaction.hide(f3);
transaction.hide(f4);
transaction.hide(f5);
}else if("f2".equals(str)){
transaction.show(f2);
transaction.hide(f1);
transaction.hide(f3);
transaction.hide(f4);
transaction.hide(f5);
}else if("f3".equals(str)){
transaction.show(f3);
transaction.hide(f2);
transaction.hide(f1);
transaction.hide(f4);
transaction.hide(f5);
}else if("f4".equals(str)){
transaction.show(f4);
transaction.hide(f2);
transaction.hide(f3);
transaction.hide(f1);
transaction.hide(f5);
}else if("f5".equals(str)){
transaction.show(f5);
transaction.hide(f2);
transaction.hide(f3);
transaction.hide(f4);
transaction.hide(f1);
}
transaction.commit();
}
private void find() {
rg1 = (RadioGroup)findViewById(R.id.rg1);
radio1 = (RadioButton)findViewById(R.id.radio1);
radio2 = (RadioButton)findViewById(R.id.radio2);
radio3 = (RadioButton)findViewById(R.id.radio3);
radio4 = (RadioButton)findViewById(R.id.radio4);
radio5 = (RadioButton)findViewById(R.id.radio5);
}
}