Android之SharedPreferences用法
16lz
2021-12-04
package com.dingxiaomiao.sharedpreferencestest; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { static final String KEY = "MyValues"; private EditText editText; SharedPreferences preferences; Editor editor ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.editText); preferences = getPreferences(Activity.MODE_PRIVATE); editor = preferences.edit(); findViewById(R.id.readbtn).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String value = preferences.getString(KEY, "没有数据"); Toast.makeText(getApplicationContext(), value, Toast.LENGTH_LONG).show(); } }); findViewById(R.id.writebtn).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { editor.putString(KEY, editText.getText().toString()); if (editor.commit()) { Toast.makeText(getApplicationContext(), "写入成功", Toast.LENGTH_LONG).show(); } } }); } }
更多相关文章
- android videoview 没有画面的一个原因
- ADT下载地址(含各版本)(转)
- android 3G pppd 调试记录
- Android(安卓)UI布局中设置了fill_parenet仍然没有全屏的解决办
- Android(安卓)UI布局中设置了fill_parenet仍然没有全屏的解决办
- android 3G pppd 调试记录。
- 监听Android应用程序是否卸载成功
- android:maxHeight,android:maxWidth失效
- android:maxHeight,android:maxWidth失效