概述
最近在找一个功能,能够在第一次打开软件的时候有一个引导的界面,然后在每次进入软件后都会有一个停留的界面,语言形容的不怎么清楚,上效果。。。。。
以上为效果图,接下来我来说一下大概的实现的方法
首先,MainActivity.java
package com.jaycee.appguide;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean mFirst = isFirstEnter(MainActivity.this,MainActivity.this.getClass().getName());
if(mFirst)
//
mHandler.sendEmptyMessageDelayed(SWITCH_GUIDACTIVITY,5000);
mHandler.sendEmptyMessage(SWITCH_GUIDACTIVITY);
//
else
//
mHandler.sendEmptyMessageDelayed(SWITCH_MAINACTIVITY,5000);
//
mHandler.sendEmptyMessage(SWITCH_MAINACTIVITY);
}
private static final String SHAREDPREFERENCES_NAME = "my_pref";
private static final String KEY_GUIDE_ACTIVITY = "guide_activity";
@SuppressLint("WorldReadableFiles")
@SuppressWarnings("deprecation")
private boolean isFirstEnter(Context context,String className){
if(context==null || className==null||"".equalsIgnoreCase(className))return false;
String mResultStr = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_WORLD_READABLE)
.getString(KEY_GUIDE_ACTIVITY, "");
if(mResultStr.equalsIgnoreCase("false"))
return false;
else
return true;
}
private final static int SWITCH_MAINACTIVITY = 1000;
private final static int SWITCH_GUIDACTIVITY = 1001;
@SuppressLint("HandlerLeak")
public Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what){
case SWITCH_MAINACTIVITY:
Intent mIntent = new Intent();
mIntent.setClass(MainActivity.this, MainActivity.class);
MainActivity.this.startActivity(mIntent);
MainActivity.this.finish();
break;
case SWITCH_GUIDACTIVITY:
mIntent = new Intent();
mIntent.setClass(MainActivity.this, GuideActivity.class);
MainActivity.this.startActivity(mIntent);
MainActivity.this.finish();
break;
}
super.handleMessage(msg);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
其相对应的布局文件为:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
然后为GuideActivity.java,其功能为实现页面的引导,
package com.jaycee.appguide;
import android.app.Activity;
import android.os.Bundle;
import java.util.ArrayList;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
public class GuideActivity extends Activity {
private ViewPager viewPager;
private ArrayList<View> pageViews;
private ImageView imageView;
private ImageView[] imageViews;
private ViewGroup viewPics;
private ViewGroup viewPoints;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList<View>();
pageViews.add(inflater.inflate(R.layout.viewpager_page1, null));
pageViews.add(inflater.inflate(R.layout.viewpager_page2, null));
pageViews.add(inflater.inflate(R.layout.viewpager_page3, null));
imageViews = new ImageView[pageViews.size()];
viewPics = (ViewGroup) inflater.inflate(R.layout.activity_guide, null);
viewPoints = (ViewGroup) viewPics.findViewById(R.id.viewGroup);
viewPager = (ViewPager) viewPics.findViewById(R.id.guidePages);
for(int i=0;i<pageViews.size();i++){
imageView = new ImageView(GuideActivity.this);
imageView.setLayoutParams(new LayoutParams(20,20));
imageView.setPadding(20, 0, 20, 0);
imageViews[i] = imageView;
if(i == 0){
imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
}else{
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
viewPoints.addView(imageViews[i]);
}
setContentView(viewPics);
viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}
private Button.OnClickListener
Button_OnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
setGuided();
Intent mIntent = new Intent();
mIntent.setClass(GuideActivity.this, WelcomeActivity.class);
GuideActivity.this.startActivity(mIntent);
GuideActivity.this.finish();
}
};
private static final String SHAREDPREFERENCES_NAME = "my_pref";
private static final String KEY_GUIDE_ACTIVITY = "guide_activity";
private void setGuided(){
SharedPreferences settings = getSharedPreferences(SHAREDPREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(KEY_GUIDE_ACTIVITY, "false");
editor.commit();
}
class GuidePageAdapter extends PagerAdapter{
@Override
public void destroyItem(View v, int position, Object arg2) {
// TODO Auto-generated method stub
((ViewPager)v).removeView(pageViews.get(position));
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pageViews.size();
}
@Override
public Object instantiateItem(View v, int position) {
// TODO Auto-generated method stub
((ViewPager) v).addView(pageViews.get(position));
if (position == 2) {
Button btn = (Button) v.findViewById(R.id.btn_close_guide);
btn.setOnClickListener(Button_OnClickListener);
}
return pageViews.get(position);
}
@Override
public boolean isViewFromObject(View v, Object arg1) {
// TODO Auto-generated method stub
return v == arg1;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
}
class GuidePageChangeListener implements OnPageChangeListener{
@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
}
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
for(int i=0;i<imageViews.length;i++){
imageViews[position].setBackgroundResource(R.drawable.page_indicator_focused);
if(position !=i){
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
}
相对应的布局为:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/viewGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/jiemian_1"
android:orientation="vertical">
<!--<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:text="哈哈,这是引导的第一个页面"
/>
-->
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/jiemian_2"
android:orientation="vertical">
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/jiemian_3"
android:gravity="center">
<Button
android:id="@+id/btn_close_guide"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="#D1D1D1"
android:text="开始体验"
android:layout_marginTop="400dp"
android:textSize="18sp"/>
</LinearLayout>
以上的就是在第一次打开软件的时候所出现的引导的界面,
接下来的是在每次打开软件的时候,都会有一个停留的界面。
package com.jaycee.appguide;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class WelcomeActivity extends Activity {
private final long SPLASH_LENGTH = 4000;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
handler.postDelayed(new Runnable(){
public void run(){
Intent intent = new Intent(WelcomeActivity.this,MainActivity.class);
startActivity(intent);
finish();
}
},SPLASH_LENGTH);
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/welcome"
tools:context="com.example.phonec.WelcomeActivity" >
</LinearLayout>
好的,以上的基本就是这样了
最后
以上就是飘逸老师为你收集整理的安卓软件第一次打开时引导界面以及打开后停留界面的全部内容,希望文章能够帮你解决安卓软件第一次打开时引导界面以及打开后停留界面所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复