码迷,mamicode.com
首页 > 其他好文 > 详细

欢迎页面(Welcome)

时间:2015-05-18 23:09:36      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

package com.example.welcome;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

//第一次运行的引导页代码
public class WelcomeActivity extends Activity implements OnPageChangeListener,
		OnClickListener {
	private Context context;
	//让用户左右切换当前的View
	private ViewPager viewPager;
	//适配器类,给ViewPager提供数据
	private PagerAdapter pagerAdapter;
	private Button startButton;
	//线性布局里的指示器
	private LinearLayout indicatorLayout;
	//声明一个视图数组列表
	private ArrayList<View> views;
	
	//图像视图指示器
	private ImageView[] indicators = null;
	//声明一个int型的images数组
	private int[] images;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//activity_welcome设置为主界面
		setContentView(R.layout.activity_welcome);
		context = this;
		// 创建WelcomeActivity的桌面快捷方式
		new CreateShut(this);
		// 设置引导图片
		//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  仅需在这设置图片 指示器和page自动添加
		//实例化images
		images = new int[] { R.drawable.welcome_01, R.drawable.welcome_02,
				R.drawable.welcome_03 };
		
		initView();
	}

	// 初始化视图
	private void initView() {
		//将视图控件与视图对象相关联
		viewPager = (ViewPager) findViewById(R.id.viewpage);
		startButton = (Button) findViewById(R.id.start_Button);
		startButton.setOnClickListener(this);
		indicatorLayout = (LinearLayout) findViewById(R.id.indicator);
		//实例化一个 ArrayList<View>类型,名称为views的对象
		views = new ArrayList<View>();
		indicators = new ImageView[images.length]; // 定义指示器数组大小
		for (int i = 0; i < images.length; i++) {
			// 循环加入图片
			ImageView imageView = new ImageView(context);
			imageView.setBackgroundResource(images[i]);
			views.add(imageView);
			// 循环加入指示器
			indicators[i] = new ImageView(context);
			indicators[i].setBackgroundResource(R.drawable.indicators_default);
			if (i == 0) {
				indicators[i].setBackgroundResource(R.drawable.indicators_now);
			}
			indicatorLayout.addView(indicators[i]);
		}
		pagerAdapter = new BasePagerAdapter(views);
		viewPager.setAdapter(pagerAdapter); // 设置适配器
		viewPager.setOnPageChangeListener(this);
	}
	//按钮的点击事件
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.start_Button) {
			SharedPreferences shared = new SharedConfig(this).GetConfig();
			Editor editor = shared.edit();
			editor.putBoolean("First", false);
			editor.commit();
			
			startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			this.finish();
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	// 监听viewpage
	@Override
	public void onPageSelected(int arg0) {
		// 显示最后一个图片时显示按钮
		if (arg0 == indicators.length - 1) {
			startButton.setVisibility(View.VISIBLE);
		} else {
			startButton.setVisibility(View.INVISIBLE);
		}
		// 更改指示器图片
		for (int i = 0; i < indicators.length; i++) {
			indicators[arg0].setBackgroundResource(R.drawable.indicators_now);
			if (arg0 != i) {
				indicators[i]
						.setBackgroundResource(R.drawable.indicators_default);
			}
		}
	}
}

 

1、欢迎页面(welcomeActivity.java)

package com.example.welcome;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

//第一次运行的引导页代码
public class WelcomeActivity extends Activity implements OnPageChangeListener,
		OnClickListener {
	private Context context;
	//让用户左右切换当前的View
	private ViewPager viewPager;
	//适配器类,给ViewPager提供数据
	private PagerAdapter pagerAdapter;
	private Button startButton;
	//线性布局里的指示器
	private LinearLayout indicatorLayout;
	//声明一个视图数组列表
	private ArrayList<View> views;
	
	//图像视图指示器
	private ImageView[] indicators = null;
	//声明一个int型的images数组
	private int[] images;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//activity_welcome设置为主界面
		setContentView(R.layout.activity_welcome);
		context = this;
		// 创建WelcomeActivity的桌面快捷方式
		new CreateShut(this);
		// 设置引导图片
		//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  仅需在这设置图片 指示器和page自动添加
		//实例化images
		images = new int[] { R.drawable.welcome_01, R.drawable.welcome_02,
				R.drawable.welcome_03 };
		
		initView();
	}

	// 初始化视图
	private void initView() {
		//将视图控件与视图对象相关联
		viewPager = (ViewPager) findViewById(R.id.viewpage);
		startButton = (Button) findViewById(R.id.start_Button);
		startButton.setOnClickListener(this);
		indicatorLayout = (LinearLayout) findViewById(R.id.indicator);
		//实例化一个 ArrayList<View>类型,名称为views的对象
		views = new ArrayList<View>();
		indicators = new ImageView[images.length]; // 定义指示器数组大小
		for (int i = 0; i < images.length; i++) {
			// 循环加入图片
			ImageView imageView = new ImageView(context);
			imageView.setBackgroundResource(images[i]);
			views.add(imageView);
			// 循环加入指示器
			indicators[i] = new ImageView(context);
			indicators[i].setBackgroundResource(R.drawable.indicators_default);
			if (i == 0) {
				indicators[i].setBackgroundResource(R.drawable.indicators_now);
			}
			indicatorLayout.addView(indicators[i]);
		}
		pagerAdapter = new BasePagerAdapter(views);
		viewPager.setAdapter(pagerAdapter); // 设置适配器
		viewPager.setOnPageChangeListener(this);
	}
	//按钮的点击事件
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.start_Button) {
			SharedPreferences shared = new SharedConfig(this).GetConfig();
			Editor editor = shared.edit();
			editor.putBoolean("First", false);
			editor.commit();
			
			startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
			overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
			this.finish();
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	// 监听viewpage
	@Override
	public void onPageSelected(int arg0) {
		// 显示最后一个图片时显示按钮
		if (arg0 == indicators.length - 1) {
			startButton.setVisibility(View.VISIBLE);
		} else {
			startButton.setVisibility(View.INVISIBLE);
		}
		// 更改指示器图片
		for (int i = 0; i < indicators.length; i++) {
			indicators[arg0].setBackgroundResource(R.drawable.indicators_now);
			if (arg0 != i) {
				indicators[i]
						.setBackgroundResource(R.drawable.indicators_default);
			}
		}
	}
}

2、mainActivity.java

package com.example.welcome;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;

 
public class MainActivity extends Activity {
	private boolean first;	//判断是否第一次打开软件
	private View view;
	private Context context;
	//动画
	private Animation animation;
	//网络
	private NetManager netManager;
	//用于访问和修改getSharedPreferences(String,int)返回偏好设置数据的一个接口;
	//shared用于保存应用程序的属性设置
	private SharedPreferences shared;
	// 进入主程序的延迟时间
	private static int TIME = 1000; 		//使	activity_main缓慢的进入							

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//调用View的inflate方法,使背景图片由中心缓慢的扩散打开
		view = View.inflate(this, R.layout.activity_main, null);
		//设置主界面
		setContentView(view);
		context = this;							//得到上下文
		shared = new SharedConfig(context).GetConfig(); 	// 得到配置文件
		netManager = new NetManager(context); 				// 得到网络管理器
	}

	@Override
	//activity_main.xml展示完了后,重新执行的代码
	protected void onResume() {
		into();
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}
	// 进入主程序的方法
	private void into() {
		if (netManager.isOpenNetwork()) {
			// 如果网络可用则判断是否第一次进入,如果是第一次则进入欢迎界面
			first = shared.getBoolean("First", true);
			// 设置动画效果是alpha,在anim目录下的alpha.xml文件中定义动画效果
			animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
			// 给view设置动画效果
			view.startAnimation(animation);
			animation.setAnimationListener(new AnimationListener() {
				@Override
				public void onAnimationStart(Animation arg0) {
				}

				@Override
				public void onAnimationRepeat(Animation arg0) {
				}

				// 这里监听动画结束的动作,在动画结束的时候开启一个线程,这个线程中绑定一个Handler,并
				// 在这个Handler中调用goHome方法,而通过postDelayed方法使这个方法延迟500毫秒执行,达到
				// 达到持续显示第一屏500毫秒的效果
				@Override
				public void onAnimationEnd(Animation arg0) {
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							Intent intent;
							//如果第一次,则进入引导页WelcomeActivity
							if (first) {
								intent = new Intent(MainActivity.this,
										WelcomeActivity.class);
							}
							//如果不是第一次,则进入引导页HomeActivity 
							else {
								intent = new Intent(MainActivity.this,
										HomeActivity.class);
							}
							startActivity(intent);
							// 设置Activity的切换效果
							overridePendingTransition(R.anim.in_from_right,
									R.anim.out_to_left);
							//结束
							MainActivity.this.finish();
						}
					},TIME);
				}
			});
		} else {
			// 如果网络不可用,则弹出对话框,对网络进行设置
			Builder builder = new Builder(context);
			builder.setTitle("没有可用的网络");
			builder.setMessage("是否对网络进行设置?");
			//按键事件的响应
			//按"确定"后的响应事件
			builder.setPositiveButton("确定",							//匿名内部类
					//当点击"确定"后由DialogInterface接口创建一个新的对话框
					new android.content.DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = null;
							try {
								String sdkVersion = android.os.Build.VERSION.SDK;
								if (Integer.valueOf(sdkVersion) > 10) {
									intent = new Intent(
											android.provider.Settings.ACTION_WIRELESS_SETTINGS);
								} else {
									intent = new Intent();
									ComponentName comp = new ComponentName(
											"com.android.settings",
											"com.android.settings.WirelessSettings");
									intent.setComponent(comp);
									intent.setAction("android.intent.action.VIEW");
								}
								MainActivity.this.startActivity(intent);
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
			builder.setNegativeButton("取消",
					new android.content.DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							MainActivity.this.finish();
						}
					});
			builder.show();
		}
	}
}

3、主程序(HomeActivity.java)

package com.example.welcome;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;

public class HomeActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//设置主界面
		setContentView(R.layout.activity_home);
		//将实例化对象与对象控件相关联
		findViewById(R.id.button).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		new SharedConfig(this).ClearConfig();
		
	}
}

4、判断网络

package com.example.welcome;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetManager {
	Context context;

	public NetManager(Context context) {
		this.context = context;
	}

	// 判断网络是否可用的方法
	public boolean isOpenNetwork() {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
		}
		return false;
	}

	// 判断WIFI网络是否可用的方法
	public boolean isOpenWifi() {
		ConnectivityManager connManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWifi = connManager
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		return mWifi.isConnected();
	}
}




 

 

欢迎页面(Welcome)

标签:

原文地址:http://blog.csdn.net/lilu920923/article/details/45825787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!