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; } } }; }

(0)

相关推荐

  • ART模式和Dalvik模式

    ART模式ART模式英文全称为:Android runtime,谷歌Android 4.4系统新增的一种应用运行模式,与传统的Dalvik模式不同,ART模式可以实现更为流畅的安卓系统体验,对于大家来 ...

  • Android studio从Github克隆下载源代码并导入的教程

    自从Android studio给开发代码的工具绑定了很多的插件,也集成了github的功能可以通过这个github直接下载到自己仓库中的源代码,然后直接加载到Android studio的开发界面中 ...

  • Android 拨号器 源代码

    Android 拨号器 源代码 操作方法 01 AndroidManifest.xml: <?xml version="1.0" encoding="utf-8&q ...

  • 九步教你安装android源代码

    稳定版的Android源代码已经公布,任何人都可以免费下载.Google希望通过公布源代码,电信运营商和手机制造商,乃至一般开发者们进一步深刻了解和利用Android系统,从而有益于该平台下的的发展. ...

  • Android Studio 快捷键整理分享

    Android Studio 下载: Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 ...

  • Android Studio导入第三方类库图文详解步骤

    本人也刚刚开始尝试做android app的开发,听说android studio是Google支持的android 应用开发工具,所以想应该肯定比Eclipse好用吧,反正以前没有java开发经验, ...

  • 在PC上运行Android游戏

    自从运行谷歌 Android 系统的手机大行其道之后,不少朋友都幻想过在普通的 PC 电脑,或者说 Windows 系统上运行 Android 操作系统,虽然有不少软件都号称可以做到运行 Androi ...

  • Android APK反编译详解(附图)

    一.反编译Apk得到Java源代码 首先要下载两个工具:dex2jar和JD-GUI 前者是将apk中的classes.dex转化成Jar文件,而JD-GUI是一个反编译工具,可以直接查看Jar包的源 ...

  • Win10一周年更新版 Win10电脑同步Android手机通知图文教程

    Win10电脑同步Android手机通知可以实现在Win10 PC上同步Android手机和Win10手机的通知,且还能够通过通知中心对部分通知进行快速回应,例如回复短信或邮件等,需要了解的用户快来看 ...