編輯:Android開發實例
SharedPreferences: 是個接口,用來存儲KEY/VALUE數據!http://developer.android.com/reference/android/content/SharedPreferences.html
它的使用很簡單分三步:
解惑:
代碼樣例:
layout file:
- <?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:orientation="vertical"
- android:paddingLeft="@dimen/activity_vertical_padding" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/username"
- android:textSize="@dimen/textview_20dp" />
- <EditText
- android:id="@+id/etName"
- android:layout_width="@dimen/edittext_width_150dp"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:inputType="text" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/habit"
- android:textSize="@dimen/textview_20dp" />
- <EditText
- android:id="@+id/etHabit"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:inputType="text" />
- <CheckBox
- android:id="@+id/cbEmployee "
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:text="@string/isEmployee" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/company"
- android:textSize="@dimen/textview_20dp" />
- <RadioGroup
- android:id="@+id/rgCompanyType"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <RadioButton
- android:id="@+id/rbCompany1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:text="@string/rbCompany1" />
- <RadioButton
- android:id="@+id/rbCompany2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:text="@string/rbCompany2" />
- <RadioButton
- android:id="@+id/rbCompany3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/activity_margin"
- android:text="@string/rbCompany3" />
- </RadioGroup>
- <Button
- android:id="@+id/shared_pre_click"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/display" />
- <TextView
- android:id="@+id/shared_pre_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint=""
- android:textIsSelectable="false" />
- </LinearLayout>
Note: strings文件,dimens文件可以自定義,這裡不給出!
Demo code:
- package com.ringcentral.demowork.storedata;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.EditText;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.ringcentral.demowork.R;
- public class UsingSharedPreferences extends Activity {
- private SharedPreferences spfPreferences = null;
- private final String fileName = "spfDemofile.xml";
- private EditText edName;
- private EditText etHabit;
- private CheckBox cbEmployee;
- private RadioGroup rGroup;
- // private RadioButton one;
- private RadioButton two;
- // private RadioButton three;
- private Button displayButton;
- private TextView displayTextView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.shared_preferences);
- spfPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
- init();
- displayButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- SharedPreferences.Editor editor = spfPreferences.edit();
- if (edName.getText().toString() == null
- || edName.getText().toString().equalsIgnoreCase("")) {
- Toast.makeText(UsingSharedPreferences.this, "請設置姓名",
- Toast.LENGTH_LONG).show();
- displayTextView.setText("");
- return;
- }
- if (!cbEmployee.isChecked()) {
- rGroup.clearCheck();
- editor.putInt("radio", -1);
- }
- editor.putInt("radio", rGroup.getCheckedRadioButtonId());
- editor.putString("Name", edName.getText().toString());
- editor.putString("Habit", etHabit.getText().toString());
- editor.putBoolean("check", cbEmployee.isChecked());
- editor.commit();
- StringBuilder sb = new StringBuilder();
- sb.append(spfPreferences.getString("Name", "ADMIN"))
- .append("\t")
- .append(spfPreferences.getString("Habit", "無"))
- .append("\t")
- .append(spfPreferences.getBoolean("check", false))
- .append("\t");
- int temp = spfPreferences.getInt("radio", -1);
- if(temp==-1){
- if(cbEmployee.isChecked()){
- Toast.makeText(UsingSharedPreferences.this, "請選擇單位性質",
- Toast.LENGTH_LONG).show();
- displayTextView.setText("");
- return;
- }
- sb.append("\t").append(" 您未上班哦!");
- }else{
- RadioButton tempButton = (RadioButton) findViewById(spfPreferences
- .getInt("radio", R.id.rbCompany2));
- sb.append(tempButton.getText().toString());
- }
- displayTextView.setText(sb.toString());
- }
- });
- }
- private void init() {
- edName = (EditText) findViewById(R.id.etName);
- etHabit = (EditText) findViewById(R.id.etHabit);
- cbEmployee = (CheckBox) findViewById(R.id.cbEmployee);
- // one = (RadioButton) findViewById(R.id.rbCompany1);
- two = (RadioButton) findViewById(R.id.rbCompany2);
- // three = (RadioButton) findViewById(R.id.rbCompany3);
- rGroup = (RadioGroup) findViewById(R.id.rgCompanyType);
- displayButton = (Button) findViewById(R.id.shared_pre_click);
- displayTextView = (TextView) findViewById(R.id.shared_pre_text);
- }
- @Override
- public void onStop() {
- SharedPreferences.Editor editor = spfPreferences.edit();
- editor.putString("Name", edName.getText().toString());
- editor.putString("Habit", etHabit.getText().toString());
- editor.putBoolean("check", cbEmployee.isChecked());
- editor.putInt("radio", rGroup.getCheckedRadioButtonId());
- editor.commit();
- super.onStop();
- }
- }
Note: 通常把數據存儲放在onStop方法中,這裡給出了二個方式:一是在onStop中存儲,二是為方便查看效果添加了一個Button並且對Button事件進行所應的處理,當前Button後,會再一下行顯示數據。
效果如下:
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
前面有文章介紹了使用GridView實現表格的方法,本文就來說說如何用ListView實現自適應的表格。GridView比ListView更容易實現自適應的表格,
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
最近寫了一個簡單的朋友圈程序,包含了朋友圈的列表實現,視頻的錄制、預覽與上傳,圖片可選擇拍照或者從相冊選取,從相冊選取可以一次選擇多張照片,並且限制照片的張數,想擁有真正