首页 > 代码库 > 15 SharePreference
15 SharePreference
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; } } }; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。