页面信息保存

页面

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bw.register.RegisterActivity"
android:gravity="center">

<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name1" />

<EditText
android:id="@+id/nametext"
android:layout_width="200dp"
android:layout_height="wrap_content" />
</TableRow>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/size" />

<EditText
android:id="@+id/sizetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<TextView
android:id="@+id/TextView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/birth"
android:paddingRight="20dp"
/>

<EditText
android:id="@+id/birthText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>

<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >

<TextView
android:id="@+id/TextView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Nation" />

<EditText
android:id="@+id/NationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/tableLayout1"
android:layout_marginTop="25dp"
android:text="@string/register2"
android:onClick="baocun"/>

<TextView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tableLayout1"
android:layout_alignLeft="@+id/tableLayout1"
android:layout_marginBottom="14dp"
android:layout_marginLeft="74dp"
android:text="@string/register"
android:textSize="25sp" />

</RelativeLayout>

 

 

 

后台

package com.bw.register;

import android.app.Activity;
import android.content.Context;
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.widget.EditText;
import android.widget.Toast;


public class RegisterActivity extends Activity {
EditText editText;
SharedPreferences shared;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
shared=getSharedPreferences("register",Context.MODE_PRIVATE);
String mingZi=shared.getString("name1","");
editText=(EditText)findViewById(R.id.nametext);
editText.setText(mingZi);

String size1=shared.getString("sizetext","");
editText=(EditText)findViewById(R.id.sizetext);
editText.setText(size1);

String birthday=shared.getString("birthday","");
editText=(EditText)findViewById(R.id.birthText);
editText.setText(birthday);

String NationSize=shared.getString("NationSize","");
editText=(EditText)findViewById(R.id.NationText);
editText.setText(NationSize);
}
public void baocun(View v){
cun();
}
public void cun(){
//先获得文本框里的内容
editText=(EditText)findViewById(R.id.nametext);
String mingzi=editText.getText().toString();

editText=(EditText)findViewById(R.id.sizetext);
String size=editText.getText().toString();

editText=(EditText)findViewById(R.id.birthText);
String birthString=editText.getText().toString();

editText=(EditText)findViewById(R.id.NationText);
String nationsString=editText.getText().toString();

shared=getSharedPreferences("register",Context.MODE_PRIVATE);
Editor editor=shared.edit();

editor.putString("name1",mingzi);

editor.putString("sizetext",size);

editor.putString("birthday",birthString);

editor.putString("NationSize",nationsString);
editor.commit();
Toast.makeText(this,"内容以保存", 1).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
cun();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.register, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

posted @ 2017-10-08 20:29  安琪儿。流苏  阅读(63)  评论(0)    收藏  举报