Android Studio 1-7 SharedPreferences存储 与 SD卡存储

今天主要学习了 SP存入SD卡 的应用 主要有如下几点

今日项目——app的引导页

最后一页时 可以选择主动跳过 或者等待五秒后自动跳转
并且 在第二次开启app时 不再显示 紧接着咱们来看看效果
运行效果

主要逻辑在于将数据存入SD卡 记得加权限 话不多说 up code

1、MainActivity布局与java代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:id="@+id/lin_id"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignBottom="@id/vp"
        android:layout_marginBottom="200dp"></LinearLayout>

</RelativeLayout>
package com.example.app2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.app2.fragment.OneFragment;
import com.example.app2.fragment.ThreeFragment;
import com.example.app2.fragment.TwoFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private List<Fragment> list = new ArrayList<>();
    private List<ImageView> image = new ArrayList<>();
    private Handler handler = new Handler();
    private LinearLayout linId;
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        linId = (LinearLayout) findViewById(R.id.lin_id);
        SharedPreferences name = getSharedPreferences("name", MODE_PRIVATE);

        flag = name.getBoolean("gg",false);

        if (flag){
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);
        }else{
            init();
            SharedPreferences.Editor edit = name.edit();
            edit.putBoolean("gg",true);
            edit.commit();
        }


    }

    private void init() {

        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = -1;
            @Override
            public void run() {


                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vp.setCurrentItem(++index);
                    }
                });

                if (index == (list.size()-1)){
                    timer.cancel();
                }

            }
        },0,3000);

        for (int i = 0; i < list.size(); i++) {

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.drw_false);
            image.add(imageView);
            linId.addView(imageView);

        }
        image.get(0).setImageResource(R.drawable.drw_true);

        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

                for (int j = 0; j < image.size(); j++) {
                    if (j == i){
                        image.get(j).setImageResource(R.drawable.drw_true);
                    }else{
                        image.get(j).setImageResource(R.drawable.drw_false);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }
}

跳转之后的页面布局与java代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="他来了 他来了"
        android:textSize="50dp"
        android:gravity="center"/>

</LinearLayout>

package com.example.app2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

2、共三页Fragmemt的布局与java代码

第一页

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.OneFragment">

    <!-- TODO: Update blank fragment layout -->
    <ImageView
        android:id="@+id/image_id1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>
package com.example.app2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.app2.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {


    public OneFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_one, container, false);

        ImageView imageView = inflate.findViewById(R.id.image_id1);

        imageView.setImageResource(R.mipmap.ic_launcher);



        return inflate;
    }

}

第二页

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".fragment.TwoFragment">

    <!-- TODO: Update blank fragment layout -->
    <ImageView
        android:id="@+id/image_id2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>
package com.example.app2.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.app2.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class TwoFragment extends Fragment {


    public TwoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_two, container, false);

        ImageView imageView = inflate.findViewById(R.id.image_id2);

        imageView.setImageResource(R.mipmap.ic_launcher);




        return inflate;
    }

}

第三页 (主要)

<?xml version="1.0" encoding="utf-8"?>
<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=".fragment.ThreeFragment">


    <ImageView
        android:id="@+id/image_id3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <Button
        android:id="@+id/btn_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_alignParentRight="true"/>

    <Button
        android:id="@+id/btn_inter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="进入"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>


</RelativeLayout>
package com.example.app2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.app2.Main2Activity;
import com.example.app2.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class ThreeFragment extends Fragment {

    private int count = 9;
    private Handler handler = new Handler();

    public ThreeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_three, container, false);

        ImageView imageView = inflate.findViewById(R.id.image_id3);
        final Button text = inflate.findViewById(R.id.btn_text);
        Button inter = inflate.findViewById(R.id.btn_inter);


        //点击按钮跳转
        inter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                startActivity(intent);
            }
        });
        //设置图片
        imageView.setImageResource(R.mipmap.ic_launcher);

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {

                count--;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        text.setText("倒计时:"+count);

                        if (count == 0){
                            Intent intent = new Intent(getActivity(), Main2Activity.class);
                            startActivity(intent);
                            getActivity().finish();
                        }
                    }
                });

            }
        },0,1000);




        return inflate;
    }

}

发布了20 篇原创文章 · 获赞 4 · 访问量 904

猜你喜欢

转载自blog.csdn.net/v1141261428/article/details/98984887
今日推荐