zoukankan      html  css  js  c++  java
  • 进阶篇-用户界面:11.启动界面和引导页面

    SplashScreen.java

    import java.io.File;
    import java.io.OutputStream;
    
    import com.baite.tools.AssetDatabaseManager;
    
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.TextView;
    
    
    
    public class SplashScreen extends Activity {
        
        /**
         * Called when the activity is first created.
         */
        private SharedPreferences preferences;  
         private Editor editor;  
          private OutputStream os;  
    
        @Override
        public void onCreate(Bundle icicle) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉活动上面的黑色bar
            super.onCreate(icicle);
            setContentView(R.layout.activity_splash_screen);
            
            
            preferences = getSharedPreferences("phone", Context.MODE_PRIVATE);  
               //判断是不是首次登录,  
               if (preferences.getBoolean("firststart", true)) { //这句没有符合判断 
                   System.out.println("首次使用!");
                   editor = preferences.edit();  
                //将登录标志位设置为false,下次登录时不在显示首次登录界面  
                editor.putBoolean("firststart", false);  
                editor.commit();  
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        /* Create an Intent that will start the Main WordPress Activity. */
                        System.out.println("进入引导页面!");
                        AssetDatabaseManager.initManager(getApplication());
                         Intent intent = new Intent(SplashScreen.this,GuidPage.class);  
                         startActivity(intent);  
                         finish(); 
                        
                    }
                  }, 2900); //2900 for release
                
               }  
               else{
                   new Handler().postDelayed(new Runnable() {
                       public void run() {
                           /* Create an Intent that will start the Main WordPress Activity. */
                           System.out.println("进入引导页面!");
                           AssetDatabaseManager.initManager(getApplication());
                            Intent intent = new Intent(SplashScreen.this,MainActivity.class);  
                            startActivity(intent);  
                            finish(); 
                           
                       }
                     }, 2900); //2900 for release
                   System.out.println("不是首次使用!");
                   
               }
                 
      
        }
    
    }

    GuidePage.java

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import com.baite.tools.ViewPagerAdapter;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ImageView.ScaleType;
    import android.widget.LinearLayout;
    
    public class GuidPage extends Activity implements OnPageChangeListener, OnClickListener {
         // 定义ViewPager对象
        private ViewPager viewPager;
        // 定义ViewPager适配器
        private ViewPagerAdapter vpAdapter;
        // 定义一个ArrayList来存放View
        private ArrayList<View> views;
        private View view;
        // 引导图片资源
        private static final int[] pics = { R.drawable.guide1, R.drawable.guide2,
                R.drawable.guide3, R.drawable.guide4 };
        // 底部小点的图片
        private ImageView[] points;
        // 记录当前选中位置
        private int currentIndex;
        
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);  //去掉活动上面的黑色bar
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_guid_page);
            
            /*Button newLife=(Button)findViewById(R.id.startnewlife);
             newLife.setOnClickListener(new OnClickListener() {
                    
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent intent=new Intent(GuidPage.this,MainBord.class);    
                        GuidPage.this.startActivity(intent);
                    }
                });*/
            //Button start =(Button)findViewById(R.id.guid2start);
            
            /*start.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent(GuidPage.this,MainBord.class);
                    GuidPage.this.startActivity(intent);
                    GuidPage.this.finish();
                    
                }
            });*/
            
            initView();
            initData();
            
        }
        public void writeFile(String fileName,String writestr) throws IOException{   //写文件
              try{   
              
                    FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);   
              
                    byte [] bytes = writestr.getBytes();   
              
                    fout.write(bytes);   
              
                    fout.close();   
                  }   
              
                    catch(Exception e){   
                    e.printStackTrace();   
                   }   
            }
        
         /**
         * 初始化组件
         */
        private void initView() {
            // 实例化ArrayList对象
            views = new ArrayList<View>();
            // 实例化ViewPager
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            // 实例化ViewPager适配器
            vpAdapter = new ViewPagerAdapter(views);
        }
    
        /**
         * 初始化数据
         */
        private void initData() {
            // 定义一个布局并设置参数
            LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
    
            // 初始化引导图片列表
            for (int i = 0; i < pics.length-1; i++) {
                ImageView iv = new ImageView(this);
                iv.setLayoutParams(mParams);
                //防止图片不能填满屏幕
                iv.setScaleType(ScaleType.FIT_XY);
                //加载图片资源
                iv.setImageResource(pics[i]);
                views.add(iv);
            }
            
            LayoutInflater inflater = LayoutInflater.from(this);
            view=inflater.inflate(R.layout.last_page, null);
            views.add(view);
            
            Button newlife= (Button)view.findViewById(R.id.startnewlife);
            newlife.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Intent intent=new Intent(GuidPage.this,MainActivity.class);
                    GuidPage.this.startActivity(intent);
                    try {
                        writeFile("flag.txt", "WXDJGKFI78DKGPOK");
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    GuidPage.this.finish();
                }
            });
            
           
    
            // 设置数据
            viewPager.setAdapter(vpAdapter);
            // 设置监听
            viewPager.setOnPageChangeListener(this);
    
            // 初始化底部小点
            initPoint();
            
            
        }
    
        /**
         * 初始化底部小点
         */
        private void initPoint() {
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
            
    
            points = new ImageView[pics.length];
    
            // 循环取得小点图片
            for (int i = 0; i < pics.length; i++) {
                // 得到一个LinearLayout下面的每一个子元素
                points[i] = (ImageView) linearLayout.getChildAt(i);
                // 默认都设为灰色
                points[i].setEnabled(true);
                // 给每个小点设置监听
                points[i].setOnClickListener(this);
                // 设置位置tag,方便取出与当前位置对应
                points[i].setTag(i);
            }
    
            // 设置当面默认的位置
            currentIndex = 0;
            // 设置为白色,即选中状态
            points[currentIndex].setEnabled(false);
        }
    
        /**
         * 滑动状态改变时调用
         */
        
        public void onPageScrollStateChanged(int arg0) {
    
        }
    
        /**
         * 当前页面滑动时调用
         */
       
        public void onPageScrolled(int arg0, float arg1, int arg2) {
    
        }
    
        /**
         * 新的页面被选中时调用
         */
       
        public void onPageSelected(int arg0) {
            // 设置底部小点选中状态
            setCurDot(arg0);
        }
    
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            setCurView(position);
            setCurDot(position);
        }
    
        /**
         * 设置当前页面的位置
         */
        private void setCurView(int position) {
            if (position < 0 || position >= pics.length) {
                return;
            }
            viewPager.setCurrentItem(position);
        }
    
        /**
         * 设置当前的小点的位置
         */
        private void setCurDot(int positon) {
            if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
                return;
            }
            points[positon].setEnabled(false);
            points[currentIndex].setEnabled(true);
    
            currentIndex = positon;
        }
    
        
    
    }

    ViewPagerAdapter.java

    package com.baite.tools;
    
    import java.util.ArrayList;
    
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.View;
    /**
     * 
     * @author YeChao
     * @功能描述:ViewPager适配器,用来绑定数据和view 
     */
    public class ViewPagerAdapter extends PagerAdapter{
        //界面列表  
        private ArrayList<View> views;  
        public ViewPagerAdapter(ArrayList<View> views)
        {
             this.views = views; 
        }
        
        /**
         * 获得当前界面数
         */
        @Override
        public int getCount() { 
             if (views != null) {  
                 return views.size();  
             }        
             else return 0;  
        }
    
        /**
         * 判断是否由对象生成界面 
         */
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);  
        }
    
        /**
         * 销毁position位置的界面 
         */
        @Override
        public void destroyItem(View container, int position, Object object) {
            ((ViewPager) container).removeView(views.get(position));     
        }
    
        /**
         * 初始化position位置的界面 
         */
        @Override
        public Object instantiateItem(View container, int position) {
            ((ViewPager) container).addView(views.get(position), 0);  
            return views.get(position);  
        }
        
    }

    activity_guid_page.xml

    <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.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="24.0dip"
            android:orientation="horizontal" >
    
            <ImageView
                android:listSelector="@drawable/selector"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:clickable="true"
                android:contentDescription="guid"
                android:padding="15.0dip"
                android:src="@drawable/selector" />
            
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:clickable="true"
                android:contentDescription="guid"
                android:padding="15.0dip"
                android:src="@drawable/selector" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:clickable="true"
                android:contentDescription="guid"
                android:padding="15.0dip"
                android:src="@drawable/selector" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:clickable="true"
                android:contentDescription="guid"
                android:padding="15.0dip"
                android:src="@drawable/selector" />
        </LinearLayout>
    
    </RelativeLayout>
  • 相关阅读:
    C++ 17
    C++ 11
    mysql统计某一个数据库中有几张表
    poj2636---Electrical Outlets(插线板)
    poj2608---几个字母映射到同一个数字
    poj2583---Series Determination
    poj2578---三个数中找出第一个大于168的
    poj2521---lose money
    poj2538---字符匹配(映射)
    poj2509---抽k根烟就换一支,求能抽烟的总数
  • 原文地址:https://www.cnblogs.com/androidNot/p/5654626.html
Copyright © 2011-2022 走看看