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

首頁(yè) > 系統(tǒng) > Android > 正文

Android中SharedPreferences簡(jiǎn)單使用實(shí)例

2019-10-22 18:26:13
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了SharedPreferences簡(jiǎn)單使用案例,供大家參考,具體內(nèi)容如下

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, "寫(xiě)入成功" , 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="寫(xiě)"/></LinearLayout>

Android,SharedPreferences

Android,SharedPreferences

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 若尔盖县| 吴江市| 台州市| 万载县| 延长县| 东兰县| 威海市| 达日县| 颍上县| 清河县| 托克托县| 钦州市| 江孜县| 镇巴县| 临朐县| 六安市| 丘北县| 海南省| 利川市| 沙田区| 贵阳市| 任丘市| 苍梧县| 偏关县| 临颍县| 阳朔县| 云林县| 呼玛县| 洛宁县| 镇平县| 会同县| 永顺县| 黔南| 玉龙| 郁南县| 威海市| 昭平县| 上犹县| 丹东市| 郎溪县| 井冈山市|