国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中SharedPreferences簡單使用實例

2019-12-12 01:56:03
字體:
來源:轉載
供稿:網友

本文實例為大家分享了SharedPreferences簡單使用案例,供大家參考,具體內容如下

MainActivity:

public class SharedPreferencesTestActivity extends Activity implements View.OnClickListener{  private EditText editText;  private TextView textView;  private Button write;  private Button read;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_shared_preferences_test);    initView();    write.setOnClickListener(this);    read.setOnClickListener(this);  }  private void initView() {    editText=(EditText)findViewById(R.id.Edit_Test);    textView=(TextView)findViewById(R.id.Text_Test);    write=(Button)findViewById(R.id.write);    read=(Button)findViewById(R.id.read);  }  @Override  public void onClick(View v) {    switch (v.getId()){      case R.id.write:        String some=editText.getText().toString();        SharedPreferences pref = SharedPreferencesTestActivity.this.getSharedPreferences("data",MODE_PRIVATE);        SharedPreferences.Editor editor = pref.edit();        editor.putString("Content",some);        editor.commit();        Toast.makeText(SharedPreferencesTestActivity.this, "寫入成功" , Toast.LENGTH_LONG).show();        editText.setText("");        break;      case R.id.read:        SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE);        String name = pre.getString("Content","");        textView.setText(name);        Toast.makeText(SharedPreferencesTestActivity.this, "讀取成功" , Toast.LENGTH_LONG).show();        break;    }  }}

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.fae.mobile.testActivity.SharedPreferencesTestActivity">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <EditText      android:textColor="@color/red"      android:background="@null"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/Edit_Test"      android:layout_weight="1"      />    <TextView      android:textColor="@color/blue"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:id="@+id/Text_Test"      android:layout_weight="1"/>  </LinearLayout>  <Button    android:layout_marginTop="25dp"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/read"    android:text="讀"/>  <Button    android:layout_marginTop="25dp"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:id="@+id/write"    android:text="寫"/></LinearLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永兴县| 陵川县| 民丰县| 长汀县| 西乌珠穆沁旗| 贡嘎县| 淳安县| 邢台县| 蒙山县| 阿坝| 朝阳县| 苍梧县| 特克斯县| 长泰县| 修武县| 神农架林区| 平定县| 象州县| 蓬莱市| 双峰县| 常州市| 逊克县| 怀柔区| 洮南市| 绍兴县| 祁东县| 仙游县| 彭山县| 平南县| 沙洋县| 岳池县| 武功县| 高雄市| 本溪| 霍邱县| 佛学| 株洲市| 疏勒县| 徐闻县| 措勤县| 临江市|