课本上的SharedPrefernece写的比较简洁(明明就是故意漏掉了好多代码)没办法,以自己目前的能力,可能还需要跳一跳才能够到,先把代码贴在这里。
main.xml
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入姓名" />
<EditText
android:id="@+id/edtName"
android:layout_below="@id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名"
/>
<Button
android:id="@+id/btnEnd"
android:layout_below="@id/edtName"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束"
/>
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnEnd"
android:layout_alignBottom="@+id/btnEnd"
android:layout_toRightOf="@+id/btnEnd"
android:text="清除信息" />
</RelativeLayout>
MainActivity
package com.Harris.sharedpreferencedemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
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 MainActivity extends Activity {
private TextView txtName;
private EditText edtName;
private Button btnEnd;
private Button btnClear;
//下面开始进入正题,写SharedPreference了,任何方法的使用都是要自己敲出来才会慢慢熟悉的
SharedPreferences preference;
String sname ;//读取数据
String msg;
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preference = getSharedPreferences("preFile", MODE_PRIVATE);//创建文件
sname = preference.getString("name", "");//读取数据
txtName = (TextView) findViewById(R.id.txtName);
edtName = (EditText) findViewById(R.id.edtName);
btnEnd = (Button)findViewById(R.id.btnEnd);
btnClear = (Button)findViewById(R.id.btnClear);
//如果为创建数据就显示输入字段
if(sname.equals("")){
txtName.setVisibility(TextView.VISIBLE);
edtName.setVisibility(TextView.VISIBLE);
btnClear.setVisibility(Button.VISIBLE);
msg="欢迎使用本程序!\n您尚未建立基本信息,请输入姓名!";
}else{//已经建立信息就显示欢迎消息
msg = "亲爱的"+sname+"你吼啊\n 欢迎再次使用本程序!";
}
//以下为构建弹出式欢迎窗口
new AlertDialog.Builder(MainActivity.this).setTitle("欢迎使用本软件").setMessage(msg)
.setPositiveButton("确定", new OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
}
}).show();
btnEnd.setOnClickListener(listener);
btnClear.setOnClickListener(listener);
}
private Button.OnClickListener listener = new Button.OnClickListener(){
public void onClick(View v) {
switch(v.getId()){
case R.id.btnEnd:
finish();
break;
case R.id.btnClear:
if(!sname.equals("")){
preference.edit().clear().commit();
Toast.makeText(MainActivity.this, "所有信息均已被清除", Toast.LENGTH_LONG).show();
}
btnClear.setVisibility(Button.INVISIBLE);
break;
}
}
};
@Override
protected void onStop() {
super.onStop();
if(sname.equals("")){//如果未建立信息就将用户输入值存盘
preference.edit().putString("name", edtName.getText().toString()).commit();
}
}
}
好像又没有什么bug了?呵呵。。。