15 SharePreference

时间:2014-04-29 10:34:47   收藏:0   阅读:407
mamicode.com,码迷
package com.szy.shared.activity;

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;
import android.widget.Toast;

public class MainActivity extends Activity
{
    private Button btnSave;
    private Button btnShow;
    private EditText editName;
    private EditText editAge;
    private TextView txtResult;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnSave = (Button) findViewById(R.id.btnSave);
        btnShow = (Button) findViewById(R.id.btnShow);
        editName = (EditText) findViewById(R.id.editName);
        editAge = (EditText) findViewById(R.id.editAge);
        txtResult=(TextView)findViewById(R.id.txtResult);
        btnSave.setOnClickListener(listener);
        btnShow.setOnClickListener(listener);
    }

    private OnClickListener listener = new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            SharedPreferences pres = MainActivity.this.getSharedPreferences("kuka", Context.MODE_PRIVATE);
            switch (v.getId())
            {
            case R.id.btnSave:
                Editor editor = pres.edit();
                editor.putString("name", editName.getText().toString());
                editor.putInt("age", Integer.valueOf(editAge.getText().toString()));
                editor.commit();
                Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
                break;
                
            case R.id.btnShow:
                String name = pres.getString("name", "NO");
                int age = pres.getInt("age", 0);
                String bir = pres.getString("bir", "NO");
                txtResult.setText("Name="+name+";age="+age+";bir="+bir);
                break;
            }
        }
    };
}
mamicode.com,码迷

 

15 SharePreference,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!