android:數據的存儲與訪問----Sharedpreferences

Sharedpreferences,保存用戶偏好數據。

SharedPreferences是Android中最容易理解的數據存儲技術,實際上SharedPreferences處理的就是一個key-value(鍵值對)SharedPreferences常用來存儲一些輕量級的數據

下面使用Sharedpreferences保存用戶登錄賬戶和密碼,以及實現在下次登錄時將用戶名賬戶和密碼顯示在文本框中。附帶實現自動登錄功能。

通過Sharedpreferences存取數據

public class PersonService {
   private Context context;

    public PersonService(Context context) {
        this.context = context;
    }
    /**
     * 
     * 將偏好信息保存在xml文本中
     * @param nameText
     * @param ageText
     */


    public void save(String username, String password ) {
       SharedPreferences sharedPreferences=context.getSharedPreferences("example", Context.MODE_PRIVATE);
       Editor editor = sharedPreferences.edit();
       editor.putString("username", username);
       editor.putString("password", password);
       editor.commit();
    }
    /**
     * 從xml文件中讀取偏好信息顯示在界面上
     * 
     * @return
     */
    public Map<String, String> getPreferences() {
        // TODO Auto-generated method stub
        Map<String, String> params = new HashMap<String, String>();
        SharedPreferences preferences = context.getSharedPreferences("example", Context.MODE_PRIVATE);
        params.put("username", preferences.getString("username", ""));
        params.put("password", String.valueOf(preferences.getString("password", "")));
        return params;
    }
}

MainActivity .java

public class MainActivity extends Activity {
    EditText  edittext1=null;
    EditText  edittext2=null;
    PersonService personService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        edittext1=(EditText) findViewById(R.id.loginedittext1);
        edittext2=(EditText) findViewById(R.id.loginedittext2);
        personService=new  PersonService(this);
        Map<String, String> params = personService.getPreferences();
        if(params.get("username")!=null
                &&!params.get("username").equals("")
                &&params.get("password")!=null
                &&!params.get("password").equals("")){

                edittext1.setText(params.get("username"));
                edittext2.setText(params.get("password"));
                if(Check(params.get("username"),params.get("password"))){
                    Intent intent = new Intent(this, main.class);
                    startActivity(intent);
                }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //驗證用戶名和密碼是否正確
    public void Login(View v){

        String  username=edittext1.getText().toString();
        String  password=edittext2.getText().toString();

        if(Check(username,password)){
            //personService.save(username, password);
            Intent intent = new Intent(this, main.class);
            startActivity(intent);
        }
    }
    private boolean Check(String username, String password) {
        // TODO Auto-generated method stub

        if(username!=null&&password!=null){
            if(username.equals("atm")&&password.equals("0425")){
                return true;
            }
        }
        return false;
    }
}

數據的存儲與訪問—-文件方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章