<?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;@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog,int which) {// TODO 自动生成的方法存根}});builder.create().show();}}});showButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根String nameValue = sp.getString("name", "");resultText.setText("姓名:" + nameValue);}});}// /onCreate}
android使用SharedPreferences保存值,值变化会提示_第1张图片

                                                                                             

更多相关文章

  1. Android WebView 使用方法,可以解决web视频播放层级问题。
  2. Android 实现分享功能的方法 分类: Android ...
  3. android 堆栈调试方法
  4. android获取短信方法1
  5. android中drawable转bitmap的两种方法
  6. Android Activity实现切换动画的两种方法
  7. android Dialog中SeekBar的使用方法
  8. android中opengl es基本方法使用说明
  9. android 沉浸式状态栏的三种方法

随机推荐

  1. android top 10 library1
  2. 如何在activity中控制屏幕点亮与关闭
  3. Android中http请求(C# MVC接收)
  4. Android如何使用WebView访问https的网站
  5. android零碎要点---android开发者的福音,5
  6. Android禁止横竖屏和解决切换屏幕时重启A
  7. android 每天定时提醒功能实现
  8. 从android游戏框架看其生命周期
  9. Android中Message机制的灵活应用(一)
  10. Android四大核心——Activity