Android 使用SharedPreferences实 源代码
Android 使用SharedPreferences实 源代码
操作方法
- 01
main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:id="@+id/nameLable" android:text="@string/name" /> <EditText android:layout_width="200px" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_toRightOf="@id/nameLable" android:layout_alignTop="@id/nameLable" android:id="@+id/name" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:id="@+id/ageLable" android:text="@string/age" /> <EditText android:layout_width="200px" android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_toRightOf="@id/ageLable" android:layout_alignTop="@id/ageLable" android:id="@+id/age" /> </RelativeLayout> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:layout_width="100px" android:layout_height="wrap_content" android:textSize="20px" android:id="@+id/save" android:text="@string/save" /> <Button android:layout_width="100px" android:layout_height="wrap_content" android:textSize="20px" android:layout_marginLeft="10px" android:layout_toRightOf="@id/save" android:layout_alignTop="@id/save" android:id="@+id/out" android:text="@string/out" /> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20px" android:id="@+id/result" /> </LinearLayout>
- 02
package cn.etc.pres; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class PreferencesActivity extends Activity { // 姓名 private EditText nameText; // 年龄 private EditText ageText; //显示结果 private TextView resultText; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取文本框 nameText = (EditText)this.findViewById(R.id.name); ageText = (EditText)this.findViewById(R.id.age); //获取文本 resultText = (TextView) this.findViewById(R.id.result); //获取按钮 Button save = (Button)this.findViewById(R.id.save); Button out = (Button)this.findViewById(R.id.out); save.setOnClickListener(listener); out.setOnClickListener(listener); } //创建事件监听 private View.OnClickListener listener = new View.OnClickListener(){ public void onClick(View v) { Button button = (Button)v; //获取实例 SharedPreferences sp = PreferencesActivity.this.getSharedPreferences("soft", Context.MODE_PRIVATE); switch (button.getId()) { case R.id.save: //获取值 String name = nameText.getText().toString(); String age = ageText.getText().toString(); Editor editor = sp.edit(); editor.putString("name", name); editor.putInt("age", new Integer(age)); editor.commit(); Toast.makeText(PreferencesActivity.this,"保存成功!",2).show(); break; case R.id.out: String namet = sp.getString("name", ""); int aget = sp.getInt("age", 1); resultText.setText("姓名:"+namet+" 年龄:"+aget); break; } } }; }