偏好设置

        偏好设置(SharedPreferences)提供了一种以键值对(K-V)的形式保存并读取持久数据的方式;
偏好设置的本质是使用xml文件保存数据,但开发人员无须考虑xml文件的解析问题,仅需像使用Map一样使用偏好设置即可;
偏好设置属于应用程序私有,仅应用程序自身可访问;
偏好设置一般用于保存用户信息、用户设置等数据量较小的数据;
偏好设置的文件保存在/data/data/应用程序包名/shared_prefs/文件夹下;


在Android系统中,使用SharedPreferences接口的对象实现偏好设置的读取与写入;
使用ContextWrapper类定义的getSharedPreferences()方法即可获取SharedPreferences接口的对象;
方法签名:
   public SharedPreferences getSharedPreferences(String name,int mode)
在写入偏好设置时,需要使用Editor对象,通过SharedPreferences的edit()方法即可获取该对象;
通过Editor的put系列方法即可写入数据,例如putString(String key,Stringvalue);

写入完成后,应该调用Editor的commit()方法提交,以完成写入过程。

                          SharedPreferences sp = getSharedPreferences();
Editor editor = sp.edit();
editor.putString("username","hello");
editor.putInt("age",34);
editor.commit();

            在获取到SharedPreferences对象之后,直接调用get系列方法即可获取所需的数据,例如String getString(String key,String defValue);
        代码:
   SharedPreferences sp = getSharedPreferences();
   String username = sp.getString("username",null);
   int age = sp.getInt("age",45);
          使用偏好设置来保存数据:
             
 布局设置:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

  <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="40dp"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/name"
        android:layout_width="259dp"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:ems="10" />
</LinearLayout>
<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年    龄:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/age"
        android:layout_width="254dp"
        android:layout_height="wrap_content"
        android:hint="请输入年龄"
        android:ems="10" />
</LinearLayout>

<Button
    android:id="@+id/submit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="20dp"
    android:onClick="save"
    android:text="保存" />

</LinearLayout>

MainActivity:

 package com.example.lianxi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
	private EditText user_name;
	private EditText user_age;
	private String FileName = "config";
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        user_name = (EditText)findViewById(R.id.name);		
        user_age = (EditText)findViewById(R.id.age);	
        /*
         * 从目标文件中获取相应的信息之后,将信息显示到控件里边
         * 
         */
        SharedPreferences sp  = getSharedPreferences(FileName, MODE_PRIVATE);
        String username = sp.getString("username", null);
        int age = sp.getInt("age", -1);
        
        if(username != null){
        	user_name.setText(username);
        }
        if(age != -1){
        	user_age.setText(age + "");
        }
    }
    /*
     * 将信息保存在目标文件夹中
     * 
     * */
    public void save(View view){
    	String username = user_name.getText().toString();
    	int age = Integer.valueOf(user_age.getText().toString());
    
      SharedPreferences sp = getSharedPreferences(FileName, MODE_PRIVATE);
      Editor editor = sp.edit();
      editor.putString("username", username);
      editor.putInt("age", age);
      editor.commit();
      
      Toast.makeText(this, "保存成功!!", Toast.LENGTH_LONG).show();
    }
    
   
}

 偏好设置实现软件的引导界面:

主界面不需要设置:

GuideActivity

   

package com.example.lianxi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewSwitcher.ViewFactory;

public class GuideActivity extends Activity {
	private ImageSwitcher mImageSwitcher;
	private int[] mImgResIds;
	private int mCurrentImageIndex;
	private PointF mDownPoint = new PointF();

	private Animation right_Left_In, right_Left_Out, left_Right_In,
			left_Right_Out;
	private String FileName;

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

		SharedPreferences sp = getSharedPreferences(FileName, MODE_PRIVATE);
		String isFirst = sp.getString("isFirstRunning", null);
		if (isFirst != null) {
			Intent intent = new Intent(this, MainActivity.class);
			startActivity(intent);
			finish();
		}
		mImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
		mImgResIds = new int[] { R.drawable.er, R.drawable.qw, R.drawable.ty,
				R.drawable.xc };

		mImageSwitcher.setFactory(new ViewFactory() {

			@Override
			public View makeView() {
				// TODO Auto-generated method stub
				ImageView v = new ImageView(getApplicationContext());
				v.setScaleType(ScaleType.FIT_XY);
				v.setImageResource(mImgResIds[mCurrentImageIndex]);
				return v;
			}
		});
		right_Left_In = AnimationUtils
				.loadAnimation(this, R.anim.right_left_in);
		right_Left_Out = AnimationUtils.loadAnimation(this,
				R.anim.right__left_out);
		left_Right_In = AnimationUtils
				.loadAnimation(this, R.anim.left_right_in);
		left_Right_Out = AnimationUtils.loadAnimation(this,
				R.anim.left_right_out);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownPoint.x = event.getX();
			break;
		case MotionEvent.ACTION_UP:
			if (event.getX() - mDownPoint.x > 10) {
				if (mCurrentImageIndex >= 1) {
					mCurrentImageIndex--;
					((ImageView) mImageSwitcher.getNextView())
							.setImageResource(mImgResIds[mCurrentImageIndex]);
					mImageSwitcher.setInAnimation(left_Right_In);
					mImageSwitcher.setOutAnimation(left_Right_Out);
					mImageSwitcher.showNext();

					if (mCurrentImageIndex == mImgResIds.length - 1) {
						findViewById(R.id.btn).setVisibility(View.VISIBLE);
					}
				}
			}
			if (mDownPoint.x - event.getX() > 10) {
				if (mCurrentImageIndex < mImgResIds.length - 1) {
					mCurrentImageIndex++;
					((ImageView) mImageSwitcher.getNextView())
							.setImageResource(mImgResIds[mCurrentImageIndex]);
					mImageSwitcher.setInAnimation(right_Left_In);
					mImageSwitcher.setOutAnimation(right_Left_Out);
					mImageSwitcher.showNext();

					if (mCurrentImageIndex == mImgResIds.length - 1) {
						findViewById(R.id.btn).setVisibility(View.VISIBLE);
					}
				}
			}
			break;

		}
		return super.onTouchEvent(event);
	}

	public void startMain(View view) {
		SharedPreferences sp = getSharedPreferences(FileName, MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("isFirstRunning", "false");
		editor.commit();

		Intent intent = new Intent(this, MainActivity.class);
		startActivity(intent);
		finish();
	}
}
<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"
    >

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
        
    </ImageSwitcher>
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="startMain"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="50dp"
        android:visibility="gone"
        android:text="开始体验"
        />

</RelativeLayout>
动画设置:

left_right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="1500"
        android:fromXDelta="-100%"
        android:toXDelta="0"/>

</set>

left_right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="1500"
        android:fromXDelta="0"
        android:toXDelta="100%"
        />

</set>

right__left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="1500"
        android:fromXDelta="0"
        android:toXDelta="-100%"/>

</set>

right_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:duration="1500"
        android:fromXDelta="100%"
        android:toXDelta="0"/>

</set>

不要忘了在AndroidMainfest.xml中将第一个界面配置为GuideActivity。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值