Android app opens for the first time, the implementation of the boot 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. Use Viewpager to switch between several pictures, draw a circle under each picture with code , and the circle will change as the picture changes.

3. In the last picture, add a button click event and go to the official interface.


Although the program is very simple, but very practical.

Look under:



We will see that the dots in the circle will change depending on the image.


Here's how to start:

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, where LinearLayout is a small circle.


Next up 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 I just added a transparent button, imitate the following "Enter immediately" button. (Here, if you have a better way, you can share a bit of 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 detailed comments

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 per page collection */private list<view> views;/** * Viewpager Small Circle below */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 Imagev Iew (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;//make every 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 current page, change small circle to IC On_carousel_02, the 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, if it becomes the last page, get its button and add a 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); Enterbtn.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);}}

The main code I finished, although relatively simple, but very practical, I hope to help everyone.



About Viewpager related knowledge, you can see this blog, the Bo speaker of the very detailed

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


My Demo:

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

Android app opens for the first time, the implementation of the boot 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.