码迷,mamicode.com
首页 > 移动开发 > 详细

安卓第二天笔记-数据保存

时间:2016-02-21 22:40:33      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:

安卓第二天笔记--数据保存

1.保存数据私有文件

 

技术分享

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:background="#0090CA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/qq"/>
    
    <!--号码  -->
    <EditText 
        android:id="@+id/et_qqNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="请输入qq号码"/>
    <!--密码  -->
    <EditText 
        android:id="@+id/et_qqPwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入qq密码"/>
    <!--记住密码复选框  -->
    <CheckBox
        android:id="@+id/cb_remember"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="记住密码"/>
<!--登录按键  -->
    <Button 
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#09A3DC"
        android:textSize="20sp"
        android:textColor="#fff"
        android:text="登录"/>
</LinearLayout>

 

Activity

/**
 * QQ 登录 保存密码到私有文件 
 * 步骤 
 * 1.获取输入的用户名与密码
 *  2.判断是否 为空,为空就给用户提示Toast 
 *  3.保存用户名与密码到文件
 *  4.数据回显
 * 
 * @author 刘楠
 * 
 *         2016-2-18下午12:39:54
 */
public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = "MainActivity";
    /*
     * QQ号码
     */
    private EditText et_qqNumber;
    /*
     * QQ密码
     */
    private EditText et_qqPwd;
    /*
     * 记住密码,复选框
     */
    private CheckBox cb_remember;
    /*
     * 登录按键
     */
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 初始化
         */
        et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
        et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
        cb_remember = (CheckBox) findViewById(R.id.cb_remember);

        btn_login = (Button) findViewById(R.id.btn_login);
        /*
         * 为登录按键设置单击事件
         */
        btn_login.setOnClickListener(this);
        
        /*
         * 回显示数据
         */
        Map<String, String> map=QQLoginUtils.getUser(this);
        
        if(map!=null){
            et_qqNumber.setText(map.get("username"));
            et_qqPwd.setText(map.get("password"));
            String isChecked = map.get("isChecked");
            if("true".equals(isChecked)){
                cb_remember.setChecked(true);
                
            }else{
            cb_remember.setChecked(false);
            }
        }
        
    }

    /**
     * 单击事件,监听器
     */
    @Override
    public void onClick(View v) {

        // 判断ID
        switch (v.getId()) {
        case R.id.btn_login:
            // 执行相应的方法
            qqLogin();
            break;

        default:
            break;
        }

    }

    /**
     * 登录方法
     */

    public void qqLogin() {
        /*
         * 获取用户名与密码
         */
        String qq = et_qqNumber.getText().toString().trim();
        String pwd = et_qqPwd.getText().toString().trim();
        // 判断是否为空
        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "亲,qq号码 与密码不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        /*
         * 判断 是否要保存
         */
        if (cb_remember.isChecked()) {
            Log.i(TAG, "保存用户名与密码");
            // 要保存
            boolean flag = QQLoginUtils.saveUser(this, qq, pwd,cb_remember.isChecked());
            // 判断 是否保存成功
            if (flag) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

            }
        } else {
             //不要保存
            Log.i(TAG, "不保存用户名与密码");
            boolean flag = QQLoginUtils.saveUser(this, "", "",cb_remember.isChecked());
            // 判断 是否保存成功
            if (flag) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

            }
        }

    }

}

 

Utils

/**
 * 保存用户名密码工具类
    
 * @author 刘楠
 *
 * 2016-2-18下午12:50:55
 */
public class QQLoginUtils {

    /**
     * 
     * @param context 上下文
     * @param name 用户名
     * @param password 密码
     * @param isChecked  记录密码状态
     * @return  是否保存成功
     */
    public static boolean saveUser(Context context, String name, String password, boolean isChecked) {
        
        OutputStream out=null;
        try {
            File file = new File(context.getFilesDir(), "user.txt");
            
            out = new FileOutputStream(file);
            
            String data = name +"#"+password+"#"+isChecked;
            out.write(data.getBytes());
            
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    
    public static Map<String, String> getUser(Context context) {
        File file = new File(context.getFilesDir(), "user.txt");
        /*
         * 判断文件是否存在
         */
        if(!file.exists()){
            return null;
        }
        /*
         * 获取文件
         */
        Map<String,String> map= new HashMap<String, String>();
        BufferedReader br=null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String data = br.readLine();
            String[] split = data.split("#");
            
            map.put("username", split[0]);
            map.put("password", split[1]);
            map.put("isChecked", split[2]);
            //返回结果
            br.close();
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        return null;
    }

}

技术分享

2.保存数据要SD卡

更改工具类

/**
 * 保存用户名密码工具类
    
 * @author 刘楠
 *
 * 2016-2-18下午12:50:55
 */
public class QQLoginUtils {

    /**
     * 
     * @param context 上下文
     * @param name 用户名
     * @param password 密码
     * @param isChecked 
     * @return  是否保存成功
     */
    public static boolean saveUser(Context context, String name, String password, boolean isChecked) {
        /*
         * 判断SD卡是否正常
         */
        String state = Environment.getExternalStorageState();
        if(!Environment.MEDIA_MOUNTED.equals(state)){
            Toast.makeText(context, "SD卡没有插入", Toast.LENGTH_SHORT).show();
            return false;
        }
        OutputStream out=null;
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "user.txt");
            
            
            
            out = new FileOutputStream(file);
            
            String data = name +"#"+password+"#"+isChecked;
            out.write(data.getBytes());
            
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    
    public static Map<String, String> getUser(Context context) {
        /*
         * 判断SD卡是否正常
         */
        String state = Environment.getExternalStorageState();
        if(!Environment.MEDIA_MOUNTED.equals(state)){
            Toast.makeText(context, "SD卡没有插入", Toast.LENGTH_SHORT).show();
            return null;
        }
        
        File file = new File(Environment.getExternalStorageDirectory(), "user.txt");
        /*
         * 判断文件是否存在
         */
        if(!file.exists()){
            return null;
        }
        /*
         * 获取文件
         */
        Map<String,String> map= new HashMap<String, String>();
        BufferedReader br=null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String data = br.readLine();
            String[] split = data.split("#");
            
            map.put("username", split[0]);
            map.put("password", split[1]);
            map.put("isChecked", split[2]);
            //返回结果
            br.close();
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        return null;
    }

}

添加写入SD卡的权限

在Manifest.xml中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

 

3.保存数据到SharedPreferences

布局文件不变

 

/**
 * QQ 登录 保存密码到SharedPreferences
 * 步骤 1.获取输入的用户名与密码
 *  2.判断是否 为空,为空就给用户提示Toast 
 *  3.保存用户名与密码到文件
 * 
 * @author 刘楠
 * 
 *         2016-2-18下午12:39:54
 */
public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = "MainActivity";
    /*
     * QQ号码
     */
    private EditText et_qqNumber;
    /*
     * QQ密码
     */
    private EditText et_qqPwd;
    /*
     * 记住密码,复选框
     */
    private CheckBox cb_remember;
    /*
     * 登录按键
     */
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 初始化
         */
        et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
        et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
        cb_remember = (CheckBox) findViewById(R.id.cb_remember);

        btn_login = (Button) findViewById(R.id.btn_login);
        /*
         * 为登录按键设置单击事件
         */
        btn_login.setOnClickListener(this);
        
        /*
         * 回显示数据
         */
        Map<String, Object> map=QQLoginUtils.getUser(this);
        
        if(map!=null){
            et_qqNumber.setText(map.get("username").toString());
            et_qqPwd.setText(map.get("password").toString());
            boolean isChecked = (Boolean) map.get("isChecked");
            cb_remember.setChecked(isChecked);
            
        }
        
    }

    /**
     * 单击事件,监听器
     */
    @Override
    public void onClick(View v) {

        // 判断ID
        switch (v.getId()) {
        case R.id.btn_login:
            // 执行相应的方法
            qqLogin();
            break;
        }

    }

    /**
     * 登录方法
     */

    public void qqLogin() {
        /*
         * 获取用户名与密码
         */
        String qq = et_qqNumber.getText().toString().trim();
        String pwd = et_qqPwd.getText().toString().trim();
        // 判断是否为空
        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "亲,qq号码 与密码不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        /*
         * 判断 是否要保存
         */
        if (cb_remember.isChecked()) {
            Log.i(TAG, "保存用户名与密码");
            // 要保存
            boolean flag = QQLoginUtils.saveUser(this, qq, pwd,cb_remember.isChecked());
            // 判断 是否保存成功
            if (flag) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

            }
        } else {
             //不要保存
            Log.i(TAG, "不保存用户名与密码");
            boolean flag = QQLoginUtils.saveUser(this, null, null,cb_remember.isChecked());
            // 判断 是否保存成功
            if (flag) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }

        }
    }

}

 

 

工具类

/**
 * 保存用户名密码工具类
    
 * @author 刘楠
 *
 * 2016-2-18下午12:50:55
 */
public class QQLoginUtils {

    /**
     * 
     * @param context 上下文
     * @param name 用户名
     * @param password 密码
     * @param isChecked 
     * @return  是否保存成功
     */
    public static boolean saveUser(Context context, String name, String password, boolean isChecked) {
        
        SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        
        Editor editor = preferences.edit();
        editor.putString("username", name);
        editor.putString("password", password);
        editor.putBoolean("isChecked", isChecked);
        
        editor.commit();
        return true;

    }
    
    /**
     * 取出数据
     * @param context
     * @return
     */
    public static Map<String, Object> getUser(Context context) {
        Map<String, Object> map= new HashMap<String, Object>();
        SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        String username = preferences.getString("username", "");
        String password = preferences.getString("password", "");
        boolean isChecked = preferences.getBoolean("isChecked", false);
        
        map.put("username", username);
        map.put("password", password);
        map.put("isChecked", isChecked);
        
        
        return map;
        
    }

}

 

 

4.保存数据到XML文件

技术分享

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="学生信息管理系统"
        android:textColor="#ff0000"
        android:textSize="28sp" />
    <!-- 学生姓名 -->

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入学生姓名" />

    <!-- 性别 -->

    <RadioGroup
        android:id="@+id/rg_gender"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_man"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/rb_felman"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="女" />
    </RadioGroup>

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#09A3DC"
        android:text="保存"
        android:textColor="#fff" />

    <Button
        android:id="@+id/btn_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="#09A3DC"
        android:text="查询"
        android:textColor="#fff" />

    <TextView 
        android:id="@+id/tv_result"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    
</LinearLayout>

 

/**
 * 学生信息管理系统
    保存学生信息到XML文件 serializer
    
    步骤:
    1.获取用户输入的用户名
    2.判断输入是否为空, 为空给用户提示,Toast
    3.使用 serializer,生成XML保存
    查询
    1.获取用户输入的姓名
    2.查找文件是否存在
    3.存在就解析pull解析,并显示,不存在就提示
    
 * @author 刘楠
 *
 * 2016-2-18下午7:50:09
 */
public class MainActivity extends Activity  implements OnClickListener{

    /*
     * 学生姓名
     */
    private EditText et_name;
    /*
     * 学生性别
     */
    private RadioGroup rg_gender;
    
    /*
     * 保存
     */
    private Button btn_save;
    /*
     * 查询
     */
    private Button btn_query;
    
    /*
     * 显示查询结果
     */
    private TextView tv_result;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (EditText) findViewById(R.id.et_name);
        rg_gender = (RadioGroup) findViewById(R.id.rg_gender);
        
        btn_save = (Button) findViewById(R.id.btn_save);
        
        btn_query = (Button) findViewById(R.id.btn_query);
        
        tv_result = (TextView) findViewById(R.id.tv_result);
        
        /*
         * 设置单击事件
         */
        btn_save.setOnClickListener(this);
        btn_query.setOnClickListener(this);
        
    }

    /**
     * 单击事件监听器
     */
    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_save:
        //保存
        save();
        break;
    case R.id.btn_query:
        //查询
        query();
        break;
    }
        
    }
    /**
     * 查询
     */
    private void query() {
        //获取用户输入的用户名
        
                String name = et_name.getText().toString().trim();
                //判断
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(this, "学生姓名不能为空", Toast.LENGTH_SHORT).show();
                    return ;
                }
                
            Map<String, String> map =    StudentUtils.getStudent(this,name);
            //tv_result;
            if(map!=null){
                
                String data="姓名:"+map.get("name")+",性别:"+map.get("gender");
                tv_result.setText(data);
            }
    }

    /**
     * 保存
     */
    private void save() {
        //获取用户输入的用户名
        
        String name = et_name.getText().toString().trim();
        //判断
        if(TextUtils.isEmpty(name)){
            Toast.makeText(this, "学生姓名不能为空", Toast.LENGTH_SHORT).show();
            return ;
        }
        
        //判断性别
        String gender="男";
        switch (rg_gender.getCheckedRadioButtonId()) {
        case R.id.rb_man:
            RadioButton rb_man = (RadioButton) findViewById(R.id.rb_man);
            gender=rb_man.getText().toString().trim();
            break;
        case R.id.rb_felman:
            RadioButton rb_felman = (RadioButton) findViewById(R.id.rb_felman);
            gender=rb_felman.getText().toString().trim();
            break;
        }
        /*
         * 开始保存文件 serializer
         */
        
        boolean flag =StudentUtils.save(this,name,gender);
        
        if(flag){
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
        }
        
    }

    

}

 

工具类

/**
 * 学生保存,查询的工具类
 * 
 * @author 刘楠
 * 
 *         2016-2-18下午8:09:29
 */
public class StudentUtils {

    public static boolean save(Context context, String name, String gender) {

        File file = new File(context.getFilesDir(), name + ".xml");
        try {
            OutputStream out = new FileOutputStream(file);
            // 获取XML生成工具serializer
            XmlSerializer serializer = Xml.newSerializer();
            // 调置输出编码
            serializer.setOutput(out, "UTF-8");
            // 设置XML头
            serializer.startDocument("UTF-8", true);

            serializer.startTag(null, "student");

            serializer.startTag(null, "name");
            serializer.text(name);
            serializer.endTag(null, "name");

            serializer.startTag(null, "gender");
            serializer.text(gender);
            serializer.endTag(null, "gender");

            serializer.endTag(null, "student");

            serializer.endDocument();

            // 关闭流
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /*
     * 查询
     */
    public static Map<String, String> getStudent(Context context, String name) {

        File file = new File(context.getFilesDir(), name + ".xml");
        if (!file.exists()) {
            Toast.makeText(context, "学生信息不存在", Toast.LENGTH_SHORT).show();
            return null;
        }
        Map<String, String> map = new HashMap<String, String>();
        try {
            InputStream in = new FileInputStream(file);
            // 使用pull解析
            XmlPullParser pullParser = Xml.newPullParser();
            // 读取编码
            pullParser.setInput(in, "UTF-8");
            // 事件
            int eventType = pullParser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                // 判断是不是开始标签
                if (eventType == XmlPullParser.START_TAG) {

                    if ("name".equals(pullParser.getName())) {

                        String stuName = pullParser.nextText();
                        map.put("name", stuName);

                    } else if ("gender".equals(pullParser.getName())) {
                        String gender = pullParser.nextText();
                        map.put("gender", gender);
                    }
                }
                // 向下移动指针
                eventType = pullParser.next();
            }
            in.close();
            return map;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}

5.保存各种权限的文件

技术分享

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:background="#0090CA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/qq"/>
    
    <!--号码  -->
    <EditText 
        android:id="@+id/et_qqNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="请输入qq号码"/>
    <!--密码  -->
    <EditText 
        android:id="@+id/et_qqPwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="请输入qq密码"/>
     <!--记住密码复选框  -->
  <!--  <CheckBox
        android:id="@+id/cb_remember"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="记住密码"/> -->
        
        <RadioGroup 
            android:id="@+id/rg_premission"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            
            <RadioButton 
                android:id="@+id/rb_private"
                android:checked="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="私有"/>
            
            <RadioButton 
                android:id="@+id/rb_readable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="可读"/>
            <RadioButton 
                android:id="@+id/rb_writable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="可写"/>
             <RadioButton 
                android:id="@+id/rb_public"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="公有"/>
            
        </RadioGroup>
<!--登录按键  -->
    <Button 
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#09A3DC"
        android:textSize="20sp"
        android:textColor="#fff"
        android:text="登录"/>
</LinearLayout>

 

 

/**
 * QQ 登录 保存密码到私有文件 
 * 步骤 1.获取输入的用户名与密码 
 * 2.判断是否 为空,为空就给用户提示Toast
 *  3.保存用户名与密码到文件
 * 
 * @author 刘楠
 * 
 *         2016-2-18下午12:39:54
 */
public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = "MainActivity";
    /*
     * QQ号码
     */
    private EditText et_qqNumber;
    /*
     * QQ密码
     */
    private EditText et_qqPwd;
    /*
     * 记住密码,复选框
     */
    //private CheckBox cb_remember;
    /*
     * 登录按键
     */
    private Button btn_login;

    
    private RadioGroup rg_premission;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*
         * 初始化
         */
        et_qqNumber = (EditText) findViewById(R.id.et_qqNumber);
        et_qqPwd = (EditText) findViewById(R.id.et_qqPwd);
    //    cb_remember = (CheckBox) findViewById(R.id.cb_remember);

        btn_login = (Button) findViewById(R.id.btn_login);
        
        rg_premission = (RadioGroup) findViewById(R.id.rg_premission);
        
        /*
         * 为登录按键设置单击事件
         */
        btn_login.setOnClickListener(this);
        
        /*
         * 回显示数据
         */
        Map<String, String> map=QQLoginUtils.getUser(this);
        
        if(map!=null){
            et_qqNumber.setText(map.get("username"));
            et_qqPwd.setText(map.get("password"));
        }
        
    }

    /**
     * 单击事件,监听器
     */
    @Override
    public void onClick(View v) {

        // 判断ID
        switch (v.getId()) {
        case R.id.btn_login:
            // 执行相应的方法
            qqLogin();
            break;

        default:
            break;
        }

    }

    /**
     * 登录方法
     */

    public void qqLogin() {
        /*
         * 获取用户名与密码
         */
        String qq = et_qqNumber.getText().toString().trim();
        String pwd = et_qqPwd.getText().toString().trim();
        // 判断是否为空
        if (TextUtils.isEmpty(qq) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(this, "亲,qq号码 与密码不能为空!", Toast.LENGTH_SHORT).show();
            return;
        }
        /*
         * 判断 要保存的类型
         */
        int radioButtonId = rg_premission.getCheckedRadioButtonId();
        boolean flag = false ;
        switch (radioButtonId) {
        
        case R.id.rb_private:
            //私有
            flag= QQLoginUtils.saveUser(this, qq, pwd,1);
            break;
            
        case R.id.rb_readable:
            //只读
            flag= QQLoginUtils.saveUser(this, qq, pwd,2);
            break;
        
        case R.id.rb_writable:
            //可写
            flag= QQLoginUtils.saveUser(this, qq, pwd,3);
            break;
            
        case R.id.rb_public:
            //可读可写
            flag= QQLoginUtils.saveUser(this, qq, pwd,4);
            break;

        
        }
    
            // 要保存
            
            // 判断 是否保存成功
            if (flag) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

            }
        

    }

}

工具类

/**
 * 保存用户名密码工具类
    
 * @author 刘楠
 *
 * 2016-2-18下午12:50:55
 */
public class QQLoginUtils {

    /**
     * 
     * @param context 上下文
     * @param name 用户名
     * @param password 密码
     * @param mode 模式 1,私有,2,只读,3,只可写,4,可读可写
     * @return  是否保存成功
     */
    public static boolean saveUser(Context context, String name, String password,int mode) {
        
        OutputStream out=null;
        try {
            
            
            switch (mode) {
            case 1:
                out =context.openFileOutput("private.txt",context.MODE_PRIVATE);
                break;
            case 2:
                out =context.openFileOutput("readable.txt", context.MODE_WORLD_READABLE);
                break;
            case 3:
                out =context.openFileOutput("writable.txt", context.MODE_WORLD_WRITEABLE);
                break;
            case 4:
                out =context.openFileOutput("public.txt", context.MODE_WORLD_READABLE+context.MODE_WORLD_WRITEABLE);
                break;

            }
            
            String data = name +"#"+password;
            out.write(data.getBytes());
            
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    
    public static Map<String, String> getUser(Context context) {
        File file = new File(context.getFilesDir(), "user.txt");
        /*
         * 判断文件是否存在
         */
        if(!file.exists()){
            return null;
        }
        /*
         * 获取文件
         */
        Map<String,String> map= new HashMap<String, String>();
        BufferedReader br=null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String data = br.readLine();
            String[] split = data.split("#");
            
            map.put("username", split[0]);
            map.put("password", split[1]);
            //返回结果
            br.close();
            return map;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //
        return null;
    }

}

 

 

6.读取各种权限的文件

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_readPrivate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取私有文件private" />
    <Button
        android:id="@+id/btn_writePrivate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入私有文件private" />
    <Button
        android:id="@+id/btn_readReadable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取可读文件readable" />
    <Button
        android:id="@+id/btn_writeReadable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入可读文件readable" />
    <Button
        android:id="@+id/btn_readWritable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取可写文件Writable" />
    <Button
        android:id="@+id/btn_writeWritable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入可写文件Writable" />
    <Button
        android:id="@+id/btn_readPublic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取公有文件Public" />
    <Button
        android:id="@+id/btn_writePublic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="写入公有文件Public" />

</LinearLayout>

 

package com.itheiam.readfile;

import java.io.BufferedReader;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileReader;

import java.io.OutputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 读取和写入各种权限下的文件 07号项目 的文件
 * 
 * @author 刘楠
 * 
 *         2016-2-18下午6:33:00
 */
public class MainActivity extends Activity implements OnClickListener {
    /*
     * 读取私有文件按键
     */
    private Button btn_readPrivate;
    /*
     * 写入私有文件按键
     */
    private Button btn_writePrivate;
    /*
     * 读取可读文件
     */
    private Button btn_readReadable;
    /*
     * 写入可读文件
     */
    private Button btn_writeReadable;
    /*
     * 读取可写文件
     */
    private Button btn_readWritable;
    /*
     * 写入可写文件
     */
    private Button btn_writeWritable;
    /*
     * 读取公有文件
     */
    private Button btn_readPublic;
    /*
     * 写入公有文件
     */
    private Button btn_writePublic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //私有文件
        btn_readPrivate = (Button) findViewById(R.id.btn_readPrivate);
        btn_writePrivate = (Button) findViewById(R.id.btn_writePrivate);
        
        //可读文件
        btn_readReadable = (Button) findViewById(R.id.btn_readReadable);
        btn_writeReadable = (Button) findViewById(R.id.btn_writeReadable);
        
        //可写文件
        btn_readWritable = (Button) findViewById(R.id.btn_readWritable);
        btn_writeWritable = (Button) findViewById(R.id.btn_writeWritable);
        //公有文件
        btn_readPublic = (Button) findViewById(R.id.btn_readPublic);
        btn_writePublic = (Button) findViewById(R.id.btn_writePublic);
        /*
         * 设置单击事件
         */
        //私有
        btn_readPrivate.setOnClickListener(this);
        btn_writePrivate.setOnClickListener(this);
        //可读
        btn_readReadable.setOnClickListener(this);
        btn_writeReadable.setOnClickListener(this);
        //可写
        btn_readWritable.setOnClickListener(this);
        btn_writeWritable.setOnClickListener(this);
        //公有
        btn_readPublic.setOnClickListener(this);
        btn_writePublic.setOnClickListener(this);
    }

    /**
     * 单击事件监听器
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_readPrivate:
            readPrivate();
            break;
        case R.id.btn_writePrivate:
            writePrivate();
            break;
        case R.id.btn_readReadable:
            readReadable();
            break;
        case R.id.btn_writeReadable:
            writeReadable();
            break;
        case R.id.btn_readWritable:
            readWritable();
            break;
        case R.id.btn_writeWritable:
            writeWritable();
            break;
        case R.id.btn_readPublic:
            readPublic();
            break;
        case R.id.btn_writePublic:
            writePublic();
            break;

        }

    }

    /**
     * 写入公有文件
     */
    private void writePublic() {
        // 获取公有文件
                File file = new File(
                        "/data/data/com.itheima.qqlogin/files/public.txt");
                if (!file.exists()) {
                    Toast.makeText(this, "private文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始写入
                        try {
                            OutputStream out = new FileOutputStream(file);

                            out.write("我写入了".getBytes());

                            out.close();
                            Toast.makeText(this, "写入public成功", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(this, "写入public失败", Toast.LENGTH_SHORT).show();
                        }
    }
    /**
     * 读取公有文件
     */
    private void readPublic() {
        // 获取公有文件
        File file = new File(
                "/data/data/com.itheima.qqlogin/files/public.txt");
                if (!file.exists()) {
                    Toast.makeText(this, "public文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始读取
                        try {
                            BufferedReader br = new BufferedReader(new FileReader(file));

                            String data = br.readLine();

                            Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                            br.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(this, "读取public文件失败", Toast.LENGTH_SHORT).show();
                        }
    }

    /**
     * 写入可写文件
     */
    private void writeWritable() {
        // 获取可写文件
                File file = new File(
                        "/data/data/com.itheima.qqlogin/files/writable.txt");
                if (!file.exists()) {
                    Toast.makeText(this, "writable文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始写入
                        try {
                            OutputStream out = new FileOutputStream(file);

                            out.write("我写入了".getBytes());

                            out.close();
                            Toast.makeText(this, "写入writable成功", Toast.LENGTH_SHORT).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(this, "写入writable失败", Toast.LENGTH_SHORT).show();
                        }
    }
    /**
     * 读取可写文件
     */
    private void readWritable() {
        // 获取可写文件
        File file = new File(
                "/data/data/com.itheima.qqlogin/files/writable.txt");
                if (!file.exists()) {
                    Toast.makeText(this, "writable文件不存在", Toast.LENGTH_SHORT).show();
                    return;
                }
                // 开始读取
                        try {
                            BufferedReader br = new BufferedReader(new FileReader(file));

                            String data = br.readLine();

                            Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                            br.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                            Toast.makeText(this, "读取writable文件失败", Toast.LENGTH_SHORT).show();
                        }
    }

    /**
     * 写入可读文件
     */
    private void writeReadable() {
        // 获取可读文件
        File file = new File(
                "/data/data/com.itheima.qqlogin/files/readable.txt");
        if (!file.exists()) {
            Toast.makeText(this, "private文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        // 开始写入
                try {
                    OutputStream out = new FileOutputStream(file);

                    out.write("我写入了".getBytes());

                    out.close();
                    Toast.makeText(this, "写入readable成功", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "写入readable失败", Toast.LENGTH_SHORT).show();
                }
    }

    /**
     * 读取可读文件
     */
    private void readReadable() {
        // 获取可读文件
        File file = new File(
                "/data/data/com.itheima.qqlogin/files/readable.txt");
        if (!file.exists()) {
            Toast.makeText(this, "private文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        // 开始读取
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));

                    String data = br.readLine();

                    Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(this, "读取readable文件失败", Toast.LENGTH_SHORT).show();
                }
    }

    /*
     * 读取私有文件
     */
    private void readPrivate() {
        // 获取私有文件
        File file = new File("/data/data/com.itheima.qqlogin/files/private.txt");
        if (!file.exists()) {
            Toast.makeText(this, "private文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        // 开始读取
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));

            String data = br.readLine();

            Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "读取private文件失败", Toast.LENGTH_SHORT).show();
        }

    }

    /*
     * 写入私有文件
     */
    private void writePrivate() {
        // 获取私有文件
        File file = new File("/data/data/com.itheima.qqlogin/files/private.txt");
        if (!file.exists()) {
            Toast.makeText(this, "private文件不存在", Toast.LENGTH_SHORT).show();
            return;
        }
        // 开始写入
        try {
            OutputStream out = new FileOutputStream(file);

            out.write("我写入了".getBytes());

            out.close();
            Toast.makeText(this, "写入private成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "写入private失败", Toast.LENGTH_SHORT).show();
        }
    }

}

 

7.获取存储空间

 

<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button 
        android:onClick="getRamSpace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取内部存储空间"/>
    
    <Button 
        android:onClick="getSdSpace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取SD卡存储空间"/>
    
    <TextView
        android:id="@+id/tv_ram"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/tv_sd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

public class MainActivity extends Activity {

    /*
     * 显示内部空间
     */
    private TextView tv_sd;
    /*
     * 显示SD卡空间
     */
    private TextView tv_ram;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_ram = (TextView) findViewById(R.id.tv_ram);
        
        tv_sd = (TextView) findViewById(R.id.tv_sd);
    }


    /**
     *  获取内部存储空间
     * @param v
     */
    public void getRamSpace(View v){
        //总空间
                long totalSpace = Environment.getRootDirectory().getTotalSpace();
                //可用空间
                long freeSpace = Environment.getRootDirectory().getFreeSpace();
            
                //已经使用的空间
                long  useSpace = totalSpace-freeSpace;
                //总空间大小
                String totalSize = Formatter.formatFileSize(this, totalSpace);
                //可用
                String freeSize = Formatter.formatFileSize(this, freeSpace);
                //已经使用
                String useSize = Formatter.formatFileSize(this, useSpace);
                
                String data="内部存储总空间:"+totalSize+", 已经使用:"+useSize+",可用空间:"+freeSize;

                tv_ram.setText(data);
        
        
    }
    /**
     *  获取SD存储空间
     * @param v
     */
    public void getSdSpace(View v){
        //总空间
        long totalSpace = Environment.getExternalStorageDirectory().getTotalSpace();
        //可用空间
        long freeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
    
        //已经使用的空间
        long  useSpace = totalSpace-freeSpace;
        //总空间大小
        String totalSize = Formatter.formatFileSize(this, totalSpace);
        //可用
        String freeSize = Formatter.formatFileSize(this, freeSpace);
        //已经使用
        String useSize = Formatter.formatFileSize(this, useSpace);
        
        String data="SD卡总空间:"+totalSize+", 已经使用:"+useSize+",可用空间:"+freeSize;

        tv_sd.setText(data);
        
        
    }

}

 

 

8.设置中心

技术分享

<LinearLayout 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"
    android:background="#000"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="声音"
            android:textColor="#fff" />

        <CheckBox
            android:id="@+id/cb_sound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#55ffffff" 
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="显示"
            android:textColor="#fff" />

        <CheckBox
            android:id="@+id/cb_display"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#55ffffff" 
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="应用"
            android:textColor="#fff" />

        <CheckBox
            android:id="@+id/cb_app"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />
    </RelativeLayout>
  <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#55ffffff" 
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"/>
    
  <SeekBar 
      android:id="@+id/sb"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:max="100"
      android:progress="30"
      />
  
     <TextView
         android:gravity="center_horizontal"
         android:id="@+id/tv_sbProgress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textColor="#fff" />
</LinearLayout>

 

 

package com.itheima.settingcenter;


import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * 使用SharedPreferences存储选项
 * 1.初始化
 * 2.设置监听器
 * 3.保存
 * 4.取出
    
 * @author 刘楠
 *
 * 2016-2-18下午8:44:14
 */
public class MainActivity extends Activity {
    /*
     * 声音复选框保存
     */
    private CheckBox cb_sound;
    /*
     * 显示复选框
     */
    private CheckBox cb_display;
    /*
     * app复选框保存
     */
    private CheckBox cb_app;
    /*
     * 进度
     */
    private SeekBar sb;
    /*
     * 声音复选框保存
     */
    private SharedPreferences soundPreferences;
    /*
     * 显示复选框
     */
    private SharedPreferences displayPreferences;
    /*
     * app复选框保存
     */
    private SharedPreferences appPreferences;
    /*
     * seekbar的进度保存
     */
    private SharedPreferences spPreferences;
    /*
     *  显示seekbar进度
     */
    private TextView tv_sbProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        cb_sound = (CheckBox) findViewById(R.id.cb_sound);
        cb_display = (CheckBox) findViewById(R.id.cb_display);
        cb_app = (CheckBox) findViewById(R.id.cb_app);
        
         sb = (SeekBar) findViewById(R.id.sb);
         
         tv_sbProgress = (TextView) findViewById(R.id.tv_sbProgress);
         
         soundPreferences = getSharedPreferences("sound", MODE_PRIVATE);
         
         boolean soundChecked = soundPreferences.getBoolean("isChecked", false);
         
         cb_sound.setChecked(soundChecked);
         displayPreferences = getSharedPreferences("display", MODE_PRIVATE);
         
         boolean displayChecked = displayPreferences.getBoolean("isChecked", false);
         cb_display.setChecked(displayChecked);
         
         appPreferences = getSharedPreferences("app", MODE_PRIVATE);
         
         boolean appChecked = appPreferences.getBoolean("isChecked", false);
         cb_app.setChecked(appChecked);
         
         
         spPreferences= getSharedPreferences("seekBar", MODE_PRIVATE);
         
         int progress = spPreferences.getInt("progress", 0);
         
         sb.setProgress(progress);

         
         /*
          * 设置监听器
          */
         cb_sound.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Editor editor = soundPreferences.edit();
                editor.putBoolean("isChecked", isChecked);
                editor.commit();
                
            }
        } );
         /*
          * 设置监听器
          */
         cb_display.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                
                Editor editor = displayPreferences.edit();
                editor.putBoolean("isChecked", isChecked);
                editor.commit();
            }
        } );
         /*
          * 设置监听器
          */
         cb_app.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            
                Editor editor = appPreferences.edit();
                editor.putBoolean("isChecked", isChecked);
                editor.commit();
            }
            
        } );
         
         /*
          * seekbar监听器
          */
         sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress2 = seekBar.getProgress();
                
                Editor editor = spPreferences.edit();
                editor.putInt("progress", progress2);
                editor.commit();
            }
            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                
                tv_sbProgress.setText(seekBar.getProgress()+"");
            }
            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                tv_sbProgress.setText(progress+"");
            }
        });
    }



    
}

 

安卓第二天笔记-数据保存

标签:

原文地址:http://www.cnblogs.com/liunanjava/p/5202421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!