android使用SharedPreferences保存值,值变化会提示

<?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

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="姓名"
            android:textSize="20px"
            android:id="@+id/nameLable" />
        <EditText android:layout_width="80px"
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@id/nameLable"
            android:layout_alignTop="@id/nameLable"
            android:layout_marginLeft="10px"
            android:id="@+id/name" />
    </RelativeLayout>
    <RelativeLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </RelativeLayout>
    <RelativeLayout
      
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="保存"
            android:id="@+id/button" />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="显示"
            android:layout_toRightOf="@+id/button"
            android:layout_alignTop="@id/button"
            android:id="@+id/showButton" />
    </RelativeLayout>
    <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:textSize="20px"
                 android:text="内容"
            android:id="@+id/showText" />
</LinearLayout>
<pre name="code" class="java">package com.example.sharedpreferences_1;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private EditText nameText;
	private EditText ageText;
	private TextView resultText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		nameText = (EditText) findViewById(R.id.name);
		resultText = (TextView) findViewById(R.id.showText);
		final SharedPreferences sp = getSharedPreferences("lyzh",
				MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE);
		Button button = (Button) findViewById(R.id.button);
		Button showButton = (Button) findViewById(R.id.showButton);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				final String newName = nameText.getText().toString();
				String oldName = sp.getString("name", "");
				if (!newName.equals(oldName)) {
					AlertDialog.Builder builder = new Builder(MainActivity.this);
					builder.setTitle("提示");
					builder.setMessage("名称发生改变,是否修改?");
					builder.setPositiveButton(
							"确定",
							new android.content.DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO 自动生成的方法存根
									Editor editor = sp.edit();
									editor.putString("name", newName);
									editor.commit();
									Toast.makeText(MainActivity.this, "保存成功",
											Toast.LENGTH_SHORT).show();
								}
							});
					builder.setNegativeButton(
							"取消",
							new android.content.DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO 自动生成的方法存根
								}
							});
					builder.create().show();

				}

			}
		});
		showButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				String nameValue = sp.getString("name", "");
				resultText.setText("姓名:" + nameValue);
			}
		});

	}// /onCreate

}
android使用SharedPreferences保存值,值变化会提示_第1张图片

 

你可能感兴趣的:(android,数据,保存)