在Android系统中提供了多种存储技术.通过这些存储技术可以将数据存储在各种存储介质上, Android 为数据存储提供了如下几种方式:
1、文件
2、 SharedPreferences( 参数 )
3、SQLite数据库
4、 内容提供者( Content provider )
5、网络
在这里我们将对sharedpreferences存储方式进行介绍。
SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(boolean、int、float、long和String)存储在应用程序的私有目录下(data/data/[包名] /shared_prefs/)自己定义的xml文件中。
SharedPreferences是以键值对来存储应用程序的配置信息的一种方式,它只能存储基本数据类型。一个程序的配置文件仅可以在本应用程序中使用,或者说只能在同一个包内使用,不能在不同的包之间使用。实际上sharedPreferences是采用了XML格式将数据存储到设备中,在DDMS 中的File Explorer中的/data/data//shares_prefs下。
使用SharedPreferences保存key-value对的步骤如下:
(1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。
(2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。
(3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。
(4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。
下面贴出我做的小项目的部分代码:
欢迎布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/welcome"
>
</LinearLayout>
欢迎界面JAVA文件
package com.devdiv.test.ui_test_viewpager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.Window;
import android.view.WindowManager;
public class appstart extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//去掉标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.appstart);
//设置要进入下一个页面和延迟时间
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent( appstart.this,UI_Test_ViewPagerActivity.class);
startActivity(intent);
//appstart.this.finish();
}
}, 1000);}
//按手机上的返回键时直接退出程序
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.finish();
}
}
导航布局(一共六个页面,贴出的是最后一个,加了一个按钮进入主界面)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/divider_horizontal_line">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/feature_guide_5" >
</ImageView>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="98dp"
android:text="开始新的旅程吧"
android:textSize="18sp"
android:onClick="welcome_start"/>
</RelativeLayout>
</FrameLayout>
导航JAVA
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;
public class UI_Test_ViewPagerActivity extends Activity {
private ViewPager mViewPager;
private ArrayList<View> mPageViews;
private ImageView mImageView;
private ImageView[] mImageViews;
//sharepreference
public static final String PREFS_NAME = "MyPrefsFile";
public static final String FIRST_RUN = "first";
private boolean first;
//该应用的主布局LinearLayout
private ViewGroup mainViewGroup;
//主布局底部指示当前页面的小圆点视图,LinearLayout
private ViewGroup indicatorViewGroup;
//定义LayoutInflater
LayoutInflater mInflater;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
//设置窗口无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置第一次进入程序时进入导航界面,以后均不进入
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean firstTime = prefs.getBoolean("first_time", true);
if (firstTime){
Editor pEditor = prefs.edit();
pEditor.putBoolean("first_time", false);
pEditor.commit();
initViewPager();
}
else {
Intent intent = new Intent(this,mainactivity.class);
startActivity(intent);
finish();}
//mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater = getLayoutInflater();
mPageViews = new ArrayList<View>();
mPageViews.add(mInflater.inflate(R.layout.item01, null));
mPageViews.add(mInflater.inflate(R.layout.item02, null));
mPageViews.add(mInflater.inflate(R.layout.item03, null));
mPageViews.add(mInflater.inflate(R.layout.item04, null));
mPageViews.add(mInflater.inflate(R.layout.item05, null));
mPageViews.add(mInflater.inflate(R.layout.item06, null));
mImageViews = new ImageView[mPageViews.size()];
mainViewGroup = (ViewGroup) mInflater.inflate(R.layout.main, null);
mViewPager = (ViewPager) mainViewGroup.findViewById(R.id.myviewpager);
indicatorViewGroup = (ViewGroup) mainViewGroup.findViewById(R.id.mybottomviewgroup);
for (int i = 0; i < mImageViews.length; i++) {
mImageView = new ImageView(UI_Test_ViewPagerActivity.this);
mImageView.setLayoutParams(new LayoutParams(20,20));
mImageView.setPadding(20, 0, 20, 0);
if (i == 0) {
mImageView.setBackgroundResource(R.drawable.page_indicator_focused);
} else {
mImageView.setBackgroundResource(R.drawable.page_indicator);
}
mImageViews[i] = mImageView;
//把指示作用的远点图片加入底部的视图中
indicatorViewGroup.addView(mImageViews[i]);
}
//注意这两种用法的区别,前者无法正常显示!!
//setContentView(R.layout.main);
setContentView(mainViewGroup);
mViewPager.setAdapter(new MyPagerAdapter());
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < mImageViews.length; i++) {
if(i == arg0) {
mImageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
mImageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
private void initViewPager() {
// TODO Auto-generated method stub
}
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return mPageViews.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(mPageViews.get(arg1));
}
@Override
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
((ViewPager) arg0).addView(mPageViews.get(arg1));
return mPageViews.get(arg1);
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub
}
@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}
@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub
}
@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub
}
}
public void welcome_start(View v) {
Intent intent1 = new Intent();
intent1.setClass(UI_Test_ViewPagerActivity.this,mainactivity.class);
startActivity(intent1);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.finish();
}
}
主界面布局
<?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:orientation="vertical"
android:background="#C6E2FF">
<Button
android:id="@+id/main_chart"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:background="@drawable/btn_normal"
android:layout_gravity="center_horizontal"
android:text="工作查询"
android:textColor="#000000"
android:textSize="18sp"
/>
<Button
android:id="@+id/main_chart"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/btn_normal"
android:layout_gravity="center_horizontal"
android:text="图表查询"
android:textColor="#000000"
android:textSize="18sp"
/>
<Button
android:id="@+id/main_set"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/btn_normal"
android:layout_gravity="center_horizontal"
android:text="设置"
android:textColor="#000000"
android:textSize="18sp"
/>
<Button
android:id="@+id/mian_aboutus"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/btn_normal"
android:layout_gravity="center_horizontal"
android:text="关于我们"
android:textColor="#000000"
android:textSize="18sp"
/>
</LinearLayout>
主界面JAVA
package com.devdiv.test.ui_test_viewpager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
public class mainactivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
setContentView(R.layout.mainactivity);
//requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.finish();
}}
以上就是最近几天的成果,里面有ViewPagede 使用,还有SharedPreference的一种使用方法,当然SharedPreference还有很多应用,比如QQ登录界面记住密码选项(示例http://www.oschina.net/question/54100_31831)等。