主界面java代码
package com.example.day11;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.example.day11.Guide.GuidePager;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent it = new Intent(this, GuidePager.class);
startActivity(it);
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
String externalStorageState = Environment.getExternalStorageState();
if( externalStorageState.equals(Environment.MEDIA_MOUNTED)){
File externalStorageDirectory = Environment.getExternalStorageDirectory();
try {
FileOutputStream fos = new FileOutputStream(new File(externalStorageDirectory, "json.text"));
fos.write("123".getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if( requestCode==101&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
}
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
引导页java代码
package com.example.day11.Guide;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.day11.R;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class GuidePager extends AppCompatActivity {
private ViewPager vp;
private List<Fragment> list;
private List<Integer> images;
private List<String> title;
private int index;
private TabLayout tabLayout;
private Handler han=new Handler();
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_guide_pager);
vp=findViewById(R.id.guide_viewPager);
tabLayout=findViewById(R.id.guide_tabLayout);
list=new ArrayList<>();
images=new ArrayList<>();
title=new ArrayList<>();
title.add("发现");
title.add("知道");
title.add("设置");
images.add(R.mipmap.image1);
images.add(R.mipmap.image2);
images.add(R.mipmap.image3);
for (int i = 0; i < 3; i++) {
GuideFragment gf = new GuideFragment();
Bundle bun = new Bundle();
bun.putInt("image",images.get(i));
gf.setArguments(bun);
list.add(gf);
}
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return list.get(i);
}
@Override
public int getCount() {
return list.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
});
tabLayout.setupWithViewPager(vp);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
if(i==list.size()-1) {
Fragment fragment = list.get(i);
View activity = fragment.getView();
text = activity.findViewById(R.id.gf_text);
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int time = 3;
@Override
public void run() {
han.post(new Runnable() {
@Override
public void run() {
text.setVisibility(View.VISIBLE);
text.setText("倒计时:" + time);
}
});
time--;
if (time <= 0) {
finish();
timer.cancel();
}
}
}, 2000, 1000);
}
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
han.post(new Runnable() {
@Override
public void run() {
vp.setCurrentItem(index);
}
});
index++;
if( index>=title.size()){
timer.cancel();
}
}
},3000,1000);
}
}
xml代码
<?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=".Guide.GuidePager"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/guide_tabLayout"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/guide_viewPager"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
引导页的fragment
package com.example.day11.Guide;
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 android.widget.TextView;
import com.example.day11.R;
public class GuideFragment extends Fragment {
public GuideFragment() {
}
private ImageView viewById;
private TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_guide, container, false);
viewById = inflate.findViewById(R.id.gf_image);
Bundle bun = getArguments();
int image = bun.getInt("image", 0);
viewById.setImageResource(image);
return inflate;
}
}
fragment布局
<?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=".Guide.GuideFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:id="@+id/gf_image"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒计时。"
android:visibility="invisible"
android:id="@+id/gf_text"
android:layout_alignParentRight="true"
/>
</RelativeLayout>