展会信息港展会大全

Android简单的存储方式SharedPreferences
来源:互联网   发布日期:2016-01-14 12:25:48   浏览:1794次  

导读:布局文件:main.xml ?xml version=1.0 encoding=utf-8? RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent !-- 姓名标签 -- TextView android:id=@+id/nameTex...

布局文件:main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<!-- 姓名标签 -->

<TextView

android:id="@+id/nameText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginTop="10px"

android:textStyle="bold"

android:text="@string/name"

/>

<!-- 姓名输入框 -->

<EditText

android:id="@+id/nameEdit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/nameText"

android:layout_alignTop="@id/nameText"

/>

<!-- 电话 -->

<TextView

android:id="@+id/telText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/nameEdit"

android:layout_alignParentLeft="true"

android:textStyle="bold"

android:text="@string/tel"

/>

<!-- 电话输入框 -->

<EditText

android:id="@+id/telEdit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_toRightOf="@id/telText"

android:layout_alignTop="@id/telText"

/>

<!-- 反馈内容 -->

<TextView

android:id="@+id/contentText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/telEdit"

android:layout_alignParentLeft="true"

android:textStyle="bold"

android:text="@string/content"

/>

<!-- 反馈输入框 -->

<EditText

android:id="@+id/contentEdit"

android:layout_width="fill_parent"

android:layout_height="120px"

android:layout_toRightOf="@id/contentText"

android:layout_alignTop="@id/contentText"

/>

</RelativeLayout>

MainActivity

import android.app.Activity;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends Activity {

private EditText nameEdit;

private EditText telEdit;

private EditText contentEdit;

private final String PREFERENCES_NAME = "feedback";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

nameEdit = (EditText) findViewById(R.id.nameEdit);

telEdit = (EditText) findViewById(R.id.telEdit);

contentEdit = (EditText) findViewById(R.id.contentEdit);

SharedPreferences mySharedPreferences = getSharedPreferences(

PREFERENCES_NAME, MODE_PRIVATE);

nameEdit.setText(mySharedPreferences.getString("name", ""));

telEdit.setText(mySharedPreferences.getString("tel", ""));

}

@Override

public void onStop() {

SharedPreferences mySharedPreferences = getSharedPreferences(

PREFERENCES_NAME, MODE_PRIVATE);

SharedPreferences.Editor editor = mySharedPreferences.edit();

editor.putString("name", nameEdit.getText().toString());

editor.putString("tel", telEdit.getText().toString());

editor.commit();

super.onStop();

}

}

赞助本站

人工智能实验室
相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港