在android开发过程中对于第一屏的和引导界面的设计是非常必要的
第一屏:就是app启动的第一个界面正如qq和微信那样
引导界面:就是在第一次安装应用打开时来告诉你该应用的功能的一般是由几个图片组成 在第一次打开之后 以后再打开就不在出现如下图:
难点:第一屏在开启2~3秒之后关闭(实际就是个Activity) 在开启app时判断是不是第一次开启从而来确定引导界面是不是还要调用 引导界面之间是可以滑动的(实际上是viewpager和fragment的结合)
上代码:
package com.qrnu.ly.mi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import com.qrnu.ly.R;
import com.qrnu.ly.guild.GuideActivity;
/**
* android android第一屏和引导界面
*
* @author
*
*/
public class FirstActivity extends Activity {
private SharedPreferences preferences;
private Editor editor;
private final int SPLASH_DISPLAY_LENGHT = 3000;//开启之后停顿3秒
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first);
preferences = getSharedPreferences("guide", Context.MODE_PRIVATE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (preferences.getBoolean("FirstActivity", true)) {
editor = preferences.edit();
editor.putBoolean("FirstActivity", false);
editor.commit();
Intent intent = new Intent();
intent.setClass(FirstActivity.this, GuideActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent();
intent.setClass(FirstActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
}, SPLASH_DISPLAY_LENGHT);
}
}
GuideActivity.class 调用引导界面
MainActivity.class进入主界面
GuideActivity代码
package com.qrnu.ly.guild;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.qrnu.ly.R;
public class GuideActivity extends FragmentActivity {
private ViewPager viewPage;
private Fragment1 mFragment1;
private Fragment2 mFragment2;
// private Fragment3 mFragment3;
private Fragment4 mFragment4;
private PagerAdapter mPgAdapter;
private RadioGroup dotLayout;
private List mListFragment = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
setContentView(R.layout.activity_guide);
initView();
viewPage.setOnPageChangeListener(new MyPagerChangeListener());
}
private void initView() {
dotLayout = (RadioGroup) findViewById(R.id.advertise_point_group);
viewPage = (ViewPager) findViewById(R.id.viewpager);
mFragment1 = new Fragment1();
mFragment2 = new Fragment2();
// mFragment3 = new Fragment3();
mFragment4 = new Fragment4();
mListFragment.add(mFragment1);
mListFragment.add(mFragment2);
// mListFragment.add(mFragment3);
mListFragment.add(mFragment4);
mPgAdapter = new ViewPagerAdapter(getSupportFragmentManager(),
mListFragment);
viewPage.setAdapter(mPgAdapter);
}
public class MyPagerChangeListener implements OnPageChangeListener {
public void onPageSelected(int position) {
}
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
((RadioButton) dotLayout.getChildAt(position)).setChecked(true);
}
}
}
在里面用到了 Fragment1 Fragment2 Fragment4
Fragment1 代码
package com.qrnu.ly.guild;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.qrnu.ly.R;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.guide_1, container, false);
}
}
Fragment2 代码
package com.qrnu.ly.guild;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.qrnu.ly.R;
public class Fragment2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.guide_2, container, false);
}
}
Fragment4代码
package com.qrnu.ly.guild;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import com.qrnu.ly.R;
import com.qrnu.ly.mi.MainActivity;
public class Fragment4 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.guide_4, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView textView = (TextView) getView().findViewById(R.id.tvInNew);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
}
});
}
}
在Fragment4中有一个Textview和点击事件是用来响应进入主界面的监听(如立即体验)如下图:
该博客可能还有一些不全面的地方,希望大家可以理解,以后会做的更好。
不喜勿喷