数据存储preferenceActivity



数据存储preferenceActivity

1、preferenceActivity.java

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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class preferenceActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	
	EditText text1;
	EditText text2;
	Button button1;
	Button button2;
	Button button3;
	SharedPreferences sharedPreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        buttonListener();
    }
    
    public void init(){
    	text1 = (EditText)findViewById(R.id.editText1);
    	text2 = (EditText)findViewById(R.id.editText2);
    	button1 = (Button)findViewById(R.id.button1);
    	button2 = (Button)findViewById(R.id.button2);
    	button3 = (Button)findViewById(R.id.button3);
    }
    
    public void buttonListener(){
    	button1.setOnClickListener(this);
    	button2.setOnClickListener(this);
    	button3.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.button1:
			setSharedPreference();
			text1.setText("已经输入数据到preference");
			text2.setText("已经输入数据到preference");
			break;
		case R.id.button2:
			text1.setText("");
			text2.setText("");
			break;
		case R.id.button3:
			getSahrePreference();
			break;
		}
	}
	//存储sharedpreferences
	public void setSharedPreference(){
		sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("username", "我的帐号");
		editor.putInt("password", 123456);
		editor.commit();//提交修改
	}
	//获得sharedpreferences的数据
	public void getSahrePreference(){
		String username = sharedPreferences.getString("username", "");
		int password = sharedPreferences.getInt("password", 0);
		String str = String.valueOf(password);
		text1.setText(username);
		text2.setText(str);
	}
	
	

2、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" >
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" >
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" >
    </Button>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2" >
    </Button>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3" >
    </Button>

</LinearLayout>


3、运行效果




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值