Android opens the application for the first time, implements the wizard interface

Source: Internet
Author: User

Reprint please indicate the source, thank http://blog.csdn.net/harryweasley/article/details/42079167


First, the idea: 1. Use preference to store data to record whether it is the first time the software is opened

2. Using Viewpager to achieve the switch between several pictures, in each picture under the code to draw a circle , the circle will follow the picture changes.

3. In the last picture. Add a button click event. Enter the official interface.


Although the program is very easy, it is very useful.

Look under:


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvsgfycnlxzwfzbgv5/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">

We'll see that the dots in the circle will change depending on the picture.


The following starts with a commentary:

First is the Activity_main.xml file

<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 "    tools:context= ". Mainactivity ">    <android.support.v4.view.viewpager        android:id=" @+id/guide_viewpager "        android: Layout_width= "Fill_parent"        android:layout_height= "fill_parent"        android:background= "@android: color/ Transparent "/>    <linearlayout        android:id=" @+id/linearlayout "android:layout_width=" Match_        Parent "        android:layout_height=" 30DP "        android:layout_alignparentbottom=" true "        android:gravity=" Center_horizontal "        android:orientation=" Horizontal ">    </LinearLayout>  </ Relativelayout>

There is a Viewpager control and a LinearLayout control, in which LinearLayout is a small circle


Next is three pager_layout1.xml. Pager_layout2.xml. Pager_layout3.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:gravity= "Center"    android:background= "@drawable/guide1"    android:orientation= "vertical" >   </ Linearlayout>

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: background= "@drawable/guide2"    android:orientation= "vertical" >   </RelativeLayout>

<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: background= "@drawable/guide3"     android:gravity= "center"    android:orientation= "vertical" >        < Button        android:id= "@+id/guide_enter"        android:layout_width= "match_parent"        android:layout_height= " 100DP "        android:layout_gravity=" Center_horizontal|bottom "        android:layout_marginbottom=" 0DP        " Android:background= "@android: color/transparent"         /></framelayout>

We can see the front two layout directly is a picture to do the background. The third one I just added a transparent button. Imitate the following "Go Now" button.

(Here, suppose you have a better way to share the ha)


Next is the Preferenceutil class, which stores the value of the Isshow

Package Com.example.guiddemo;import Android.content.context;import Android.content.sharedpreferences;public class Preferenceutil {/**     * Whether the Welcome interface is displayed, True indicates display, false means not display */public    static final String show_guide = "ShowGuide" /** * Save to preference */public static void SetBoolean (context context, String key, Boolean value) {//Get SHAREDPREFERENCESSH Aredpreferences preferences = context.getsharedpreferences ("preference", context.mode_private); Sharedpreferences.editor Editor = Preferences.edit (); Editor.putboolean (key, value); Editor.commit ();} /** * Remove data from preference */public static Boolean Getboolean (context context, String key) {sharedpreferences preferences = con Text.getsharedpreferences ("preference", context.mode_private);//returns the key value, the default value of the key value is Falsereturn Preferences.getboolean (key, False);}}

About preference storing and extracting data I don't think I need to say more.


The main function is in the mainactivity, My code is written in the specific gaze

Package Com.example.guiddemo;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.content.intent;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.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup.layoutparams;import Android.widget.ImageView; Import Android.widget.linearlayout;import Android.widget.textview;public class Mainactivity extends Activity {/** * Whether to display the boot interface */boolean isshow = false;/** * Viewpager object */private viewpager mviewpager;/** * Load Small circle LinearLayout */private Line Arlayout indicatorlayout;/** * Viewpager each page collection */private list<view> views;/** * Viewpager below the small circle */private imageview[] mimageviews;/** * Pageradapter object */private mypageradapter mypageradapter; @Overrideprotected void OnCreate ( Bundle SavedinstancEState) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Get preference stored isshow data Isshow = Preferenceutil.getboolean (this, preferenceutil.show_guide);//isshow=false; if (isshow) {Initlog ()} is used when debugging,} else {Initview ();}} /** * Enter login interface */private void Initlog () {startactivity (new Intent (this, logactivity.class)); Finish ();} /** * Enter the boot interface */private void Initview () {Mviewpager = (Viewpager) Findviewbyid (r.id.guide_viewpager); indicatorlayout = (Li Nearlayout) Findviewbyid (r.id.linearlayout); Layoutinflater Inflater = Layoutinflater.from (this), views = new arraylist<view> (); Views.add (Inflater.inflate ( R.LAYOUT.PAGER_LAYOUT1, null)); Views.add (inflater.inflate (R.LAYOUT.PAGER_LAYOUT2, null)); Views.add ( Inflater.inflate (R.LAYOUT.PAGER_LAYOUT3, null)); mypageradapter = new Mypageradapter (this, views); mimageviews = new Imageview[views.size ()];d rawcircl (); Mviewpager.setadapter (Mypageradapter); Mviewpager.setonpagechangelistener ( New Guidepagechangelistener ());} /** * Draw Circle */private VoID DRAWCIRCL () {int num = views.size (); for (int i = 0; i < num; i++) {//instantiate each mimageviews[i]mimageviews[i] = new Imagevi EW (this); if (i = = 0) {//The first photo is selected by default, so the first small circle becomes Icon_carousel_02mimageviews[i].setimageresource (r.drawable.icon_ CAROUSEL_02);} else {mimageviews[i].setimageresource (r.drawable.icon_carousel_01);} For each small circle set the interval mimageviews[i].setpadding (7, 7, 7, 7); Linearlayout.layoutparams params = new Linearlayout.layoutparams (layoutparams.wrap_content, LayoutParams.WRAP_ CONTENT);p arams.gravity = gravity.center_vertical;//let each small circle in LinearLayout Center_ VERTICAL (middle vertical) indicatorlayout.addview (mimageviews[i], params);}} /** * * @author Harry page Change Listener event */private class Guidepagechangelistener implements Onpagechangelistener {public void Onpag escrollstatechanged (int arg0) {}public void onpagescrolled (int arg0, float arg1, int arg2) {}/** * page has changed. If it is the current page, change the small circle to icon_carousel_02. Other pages are changed to icon_carousel_01 */public void onpageselected (int arg0) {for (int i = 0; i < mimageviews.length; i++) {if (ARG0! = i) {mimageviews[i].setimageresource (r.drawable.icon_carousel_01);} else {Mimageviews[arg0].setimageresource ( r.drawable.icon_carousel_02);}}} Class Mypageradapter extends Pageradapter {private list<view> mviews;private Activity mcontext;public Mypageradapter (Activity context, list<view> views) {This.mviews = Views;this.mcontext = Context;} @Overridepublic int GetCount () {return mviews.size ();} @Overridepublic boolean isviewfromobject (View arg0, Object arg1) {return arg0 = = arg1;} @Overridepublic int GetItemPosition (Object object) {return Super.getitemposition (object);} @Overridepublic void Destroyitem (View arg0, int arg1, Object arg2) {(Viewpager) arg0). Removeview (Mviews.get (Arg1));} /** * Instantiate a page card, assuming it becomes the last page. Then get its button and join the Click event */@Overridepublic Object Instantiateitem (View arg0, int arg1) {(Viewpager) arg0). AddView ( Mviews.get (Arg1), 0); if (arg1 = = Mviews.size ()-1) {TextView enterbtn = (TextView) Arg0.findviewbyid (r.id.guide_enter); e Nterbtn.setonclicklistener (New OnclicklistEner () {@Overridepublic void OnClick (View v) {//Saves Isshow as true and enters the login interface Preferenceutil.setboolean (Mcontext, Preferenceutil.show_guide, True); Initlog ();}}); Return Mviews.get (ARG1);}}

I'm done with the main code. Albeit simpler. But it is very useful, I hope to help everyone.



About Viewpager related knowledge, you can see this blog, the Bo speaker is very specific

http://blog.csdn.net/wangjinyu501/article/details/8169924


My Demo:

http://download.csdn.net/detail/harryweasley/8278633

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Android opens the application for the first time, implements the wizard interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.