分享

Android功能引导界面实现 – Android开发中文站

 WindySky 2016-04-20

一.界面实现:

借鉴了别人的实例,然后记录下引导界面的实现,总体来说实现不算困难,前提是要有个美工帮你做这些引导图片(找了张图片凑合用吧):

主界面:

1
2
3
4
5
6
7
8
9
public class MainActivity extends PromptActivity {
    //activity的生命周期
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         setGuideResId(R.drawable.prompt2);// 添加引导页
    }
}

引导界面:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class PromptActivity extends Activity {
     private int guideResourceId = 0;// 引导页图片资源id
     private PromptSharedPreferences psp;
     @Override
     protected void onStart() {
          super.onStart();
          addGuideImage();// 添加引导页
     }
     //显示引导图片
     public void addGuideImage() {
          psp = new PromptSharedPreferences();
          View view = getWindow().getDecorView().findViewById(R.id.my_content_view);// 查找通过setContentView上的根布局
          if (view == null)
               return;
          if (psp.takeSharedPreferences(this)) {
               // 有过功能引导,就跳出
               return;
          }
          ViewParent viewParent = view.getParent();
          if (viewParent instanceof FrameLayout) {
                final FrameLayout frameLayout = (FrameLayout) viewParent;
                if (guideResourceId != 0) {
                     // 设置了引导图片
                     final ImageView guideImage = new ImageView(this);
                     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                     ViewGroup.LayoutParams.FILL_PARENT,
                     ViewGroup.LayoutParams.FILL_PARENT);
                     guideImage.setLayoutParams(params);
                     guideImage.setScaleType(ScaleType.FIT_XY);
                     guideImage.setImageResource(guideResourceId);
                     guideImage.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                                //删除引导图片
                                frameLayout.removeView(guideImage);
                                //保存记录
                                psp.saveSharedPreferences(PromptActivity.this"启动了");
                           }
                     });
                     frameLayout.addView(guideImage);// 添加引导图片
               }
         }
    }
    //获得图片id
    protected void setGuideResId(int resId) {
         this.guideResourceId = resId;
    }
}

布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:id="@id/my_content_view" >
      <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="58dp"
            android:layout_marginLeft="150dp"
            android:text="哈哈哈哈" />
     <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="58dp"
            android:layout_marginLeft="275dp"
            android:text="哈" />
</LinearLayout>

本地文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class PromptSharedPreferences {
     private SharedPreferences sp;
     // 保存
     public void saveSharedPreferences(Context context, String save) {
          sp = context.getSharedPreferences("prompt", context.MODE_PRIVATE);
          Editor editor = sp.edit();
          editor.putString("state", save);
          editor.commit();// 保存新数据
     }
     // 取出
     public boolean takeSharedPreferences(Context context) {
          String str = "";
          sp = context.getSharedPreferences("prompt", context.MODE_PRIVATE);
          str = sp.getString("state""");
          if (str.equals("")) {
                return false;
          }else{
                return true;
          }
     }
}

添加values/ids.xml:

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
     <resources>
          <item type="id" name="my_content_view"></item>
     </resources>
</xml>

转载请注明:Android开发中文站 ? Android功能引导界面实现

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多