Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 數據的讀取與寫入2

Android 數據的讀取與寫入2

編輯:關於Android編程

完成效果:選擇選項後退出,界面記住選擇選項。

1.利用SharePreferences方法記住選擇的選項

主布局:

 



    

        
            
                
布局效果:

 

\
代碼:

 


package com.example.administrator.jreduch09.Class; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import com.example.administrator.jreduch09.R; public class TestActivity extends AppCompatActivity { private Button save; private CheckBox cb1,cb2,cb3,cb4,cb5,cb6; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.homework); save= (Button) findViewById(R.id.save); cb1= (CheckBox) findViewById(R.id.cb1); cb2= (CheckBox) findViewById(R.id.cb2); cb3= (CheckBox) findViewById(R.id.cb3); cb4= (CheckBox) findViewById(R.id.cb4); cb5= (CheckBox) findViewById(R.id.cb5); cb6= (CheckBox) findViewById(R.id.cb6); op(); sp=getSharedPreferences("set.txt",MODE_APPEND); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor=sp.edit(); editor.putBoolean("1",cb1.isChecked()); editor.putBoolean("2",cb2.isChecked()); editor.putBoolean("3",cb3.isChecked()); editor.putBoolean("4",cb4.isChecked()); editor.putBoolean("5",cb5.isChecked()); editor.putBoolean("6",cb6.isChecked()); editor.commit(); } }); } public void op(){ Boolean c1=sp.getBoolean("1",false); Boolean c2=sp.getBoolean("2", false); Boolean c3=sp.getBoolean("3",false); Boolean c4=sp.getBoolean("4", false); Boolean c5=sp.getBoolean("5",false); Boolean c6=sp.getBoolean("6", false); cb1.setChecked(c1); cb2.setChecked(c2); cb3.setChecked(c3); cb4.setChecked(c4); cb5.setChecked(c5); cb6.setChecked(c6); } }
2.將選擇的選項寫入到Sd卡中 代碼:

 

 

package com.example.administrator.jreduch09;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.jreduch09.Class.shezhi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;

public class SettingActivity extends AppCompatActivity {
     private Button save;
     private CheckBox cb1,cb2,cb3,cb4,cb5,cb6;
     private TextView word;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homework);
        save= (Button) findViewById(R.id.save);
        cb1= (CheckBox) findViewById(R.id.cb1);
        cb2= (CheckBox) findViewById(R.id.cb2);
        cb3= (CheckBox) findViewById(R.id.cb3);
        cb4= (CheckBox) findViewById(R.id.cb4);
        cb5= (CheckBox) findViewById(R.id.cb5);
        cb6= (CheckBox) findViewById(R.id.cb6);
        word= (TextView) findViewById(R.id.word);
        readFile();
        word.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("選擇字體大小");
                builder.setIcon(R.mipmap.ic_launcher);
                final String [] items={"大","中","小"};
                builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Toast.makeText(getBaseContext(), items[i].toString(),
                                Toast.LENGTH_SHORT).show();
                        word.setText(items[i].toString());
                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shezhi sz =new shezhi();
                sz.setCb1(cb1.isChecked());
                sz.setCb2(cb2.isChecked());
                sz.setCb3(cb3.isChecked());
                sz.setCb4(cb4.isChecked());
                sz.setCb5(cb5.isChecked());
                sz.setCb6(cb6.isChecked());
                sz.setWord(word.getText().toString());
                String state= Environment.getExternalStorageState();
                if(!state.equals(Environment.MEDIA_MOUNTED)){
                    return;
                }
                File root=Environment.getExternalStorageDirectory();
                FileOutputStream fos=null;
                ObjectOutputStream oos=null;

                try {
                    fos=new FileOutputStream(root+"/shezhi.txt");
                    oos=new ObjectOutputStream(fos);
                    oos.writeObject(sz);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(oos!=null){
                        try {
                            oos.flush();
                            oos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
    public void readFile(){
        File root=Environment.getExternalStorageDirectory();
        FileInputStream fis=null;
        ObjectInputStream ois=null;
        try {
            fis=new FileInputStream(root+"/shezhi.txt");
            ois=new ObjectInputStream(fis);
            shezhi sz= (shezhi) ois.readObject();
            cb1.setChecked(sz.isCb1());
            cb2.setChecked(sz.isCb2());
            cb3.setChecked(sz.isCb3());
            cb4.setChecked(sz.isCb4());
            cb5.setChecked(sz.isCb5());
            cb6.setChecked(sz.isCb6());
            word.setText(sz.getWord());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(ois!=null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
效果展示:

 

\
\

\

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved