这是我第一次接触到android学习,现在模仿拉手网的welcomeactivity
activity_welcome.xml
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="indicator_right_padding">10dp</dimen>
<dimen name="indicator_corner_radius">12dp</dimen>
<dimen name="indicator_internal_padding">4dp</dimen>
<dimen name="header_footer_left_right_padding">24dp</dimen>
<dimen name="header_footer_top_bottom_padding">12dp</dimen>
</resources>
welcomeactivity.java
package com.lzh.lashou;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
public class WelcomeActivity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) { //注意新版本中onCreate的方法两个参数去掉后面的参数,否则调试后会出现空白页
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//3s后进入mainactivity
new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
return true;
}
}).sendEmptyMessageDelayed(0,3000);/*延迟3s*/
}
}
所有的页面都要在
mainifest中注册
//设置起始页为欢迎页面
<activity android:name=".welcomeactivity" />
<activity android:name=".mainactivity"/>
出现的问题,大部分就是调试后出现空白页,解决方法为重载onCreate()方法传参时只传递第一个,后一个参数删除。
下面开始进入引导页
创建yindaoactivity.java + activity_yindao.xml 同时别忘了在manifest.xml中注册,为了判断用户是否为第一次进入页面,所以在项目中添加util.ToolKits(工具类)
public class ToolKits{
public static SharedPerferences getSharedPerferences(Context context){
return context.getSharesPerferences("包名",Context.MODE_PRIVATE)//设计为私有模式
}
//设置共享参数
public static void putBoolean(Context context,String key,boolean value){
SharedPerferences sharedPerferences=getSharedPerferences(context);
//获取共享参数的编辑器
SharedPerferences.Editor editor=sharedPerences.edit();
//通过编辑器提交一个boolean类型的参数
editor.putBoolean(key,value);
editor.commit();
}
//获取共享参数
public static boolean GetBoolean(Context context,String key,boolean defaultValue){
return getSharedPreferences(context).getBoolean(key,defaultValue);
}
}
在welcome中添加判断,调用ToolKits
public class WelcomeActivity extends Activity {
public static final String IS_FIRST="is_first";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
if(ToolKits.GetBoolean(WelcomeActivity.this,IS_FIRST,false))
{
//如果默认值为false,则没有登陆过,跳转到引导页
startActivity(new Intent(WelcomeActivity.this,yindaoactivity.class));
//将boolean值设置为true
ToolKits.PutBoolean(WelcomeActivity.this,IS_FIRST,true);
}else{
//否则跳转为主页
startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
}
ToolKits.PutBoolean(WelcomeActivity.this,IS_FIRST,true);
return true;
}
}).sendEmptyMessageDelayed(0,3000);/*延迟3s*/
}
新建一个视配器
//视配器
public class GuideAdapter extends PagerAdapter {
private List<View> mList;
public GuideAdapter(List<View> list){
mList=list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view==object;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
((ViewPager)container).removeView(mList.get(position));//将指定位置的view去掉
}
//重写初始化,将图片添加到指定位置
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
((ViewPager)container).addView(mList.get(position));
return mList.get(position);
}
}
修改引导页
public class yindaoactivity extends Activity {
private ViewPager pager;
private Button btnstart;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yindao);
pager=(ViewPager)findViewById(R.id.view_pager);
btnstart=(Button) findViewById(R.id.start_btn);
//为button添加监听事件,点击后切换到主界面
btnstart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(yindaoactivity.this,MainActivity.class));
}
});
//初始化ViewPage中的3张图片
initViewPate();
}
private void initViewPate(){
//List存放3张图片
List<View> list=new ArrayList<View>();
ImageView img1=new ImageView(this);
//设置这几张图片的源
img1.setImageResource(R.drawable.guide_1);
list.add(img1);
ImageView img2=new ImageView(this);
img2.setImageResource(R.drawable.guide_2);
list.add(img2);
ImageView img3=new ImageView(this);
img3.setImageResource(R.drawable.guide_3);
list.add(img3);
GuideAdapter myAdapter=new GuideAdapter(list);
pager.setAdapter(myAdapter);
//前两张没有"立即体验"的按钮,等到第三张才有
//设置pager的监听器
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
//如果是第三张图片,显示btn
if(position==2)btnstart.setVisibility(View.VISIBLE);
else btnstart.setVisibility(View.GONE);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
对于以上使用的图片素材,可以通过反编译killer获取