欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年10月26日 10:25:14   作者:JustingWang_1  
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了SharedPreferences簡單使用案例,供大家參考,具體內(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, "寫入成功" , 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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 二維碼掃描和生成二維碼功能

    Android 二維碼掃描和生成二維碼功能

    二維碼,我們也稱作QRCode,QR表示quick response即快速響應(yīng),在很多App中我們都能見到二維碼的身影,最常見的莫過于微信了。下面通過本文給大家講解Android 二維碼掃描和生成二維碼功能,需要的朋友參考下吧
    2017-12-12
  • Android點(diǎn)擊事件派發(fā)機(jī)制源碼分析

    Android點(diǎn)擊事件派發(fā)機(jī)制源碼分析

    這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件派發(fā)機(jī)制源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • android 應(yīng)用退出時不播放動畫的解決方法

    android 應(yīng)用退出時不播放動畫的解決方法

    在Android應(yīng)用中,默認(rèn)情況下,當(dāng)用戶點(diǎn)擊返回按鈕退出應(yīng)用時,系統(tǒng)會為應(yīng)用添加一個默認(rèn)的退出動畫效果,本文將介紹如何在Android應(yīng)用中禁止退出動畫的播放,感興趣的朋友一起看看吧
    2024-05-05
  • Android基于HttpUrlConnection類的文件下載實(shí)例代碼

    Android基于HttpUrlConnection類的文件下載實(shí)例代碼

    本文通過實(shí)例代碼給大家介紹了Android基于HttpUrlConnection類的文件下載功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧
    2017-09-09
  • Kotlin中使用Dagger2可能遇到的坑解決

    Kotlin中使用Dagger2可能遇到的坑解決

    在Android上創(chuàng)建去耦以及容易測試代碼的幾乎每位遲早都要訴諸Dagger,在Kotlin中設(shè)置Dagger有一些不同,所以下面這篇文章主要給大家介紹了關(guān)于Kotlin中使用Dagger2可能遇到的坑的解決方法,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android仿高德首頁三段式滑動效果的示例代碼

    Android仿高德首頁三段式滑動效果的示例代碼

    很多app都會使用三段式滑動,比如說高德的首頁和某寶等物流信息都是使用的三段式滑動方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下
    2022-01-01
  • Android9.0上針對Toast的特殊處理圖文詳解

    Android9.0上針對Toast的特殊處理圖文詳解

    這篇文章主要給大家介紹了關(guān)于Android9.0上針對Toast的特殊處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析

    Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析

    這篇文章主要介紹了Android開發(fā)中畫廊視圖Gallery的兩種使用方法,結(jié)合實(shí)例形式分析了Android畫廊視圖Gallery的簡單布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果

    Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android MediaPlayer音頻播放器封裝示例淺析

    Android MediaPlayer音頻播放器封裝示例淺析

    Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內(nèi)置的媒體播放器的服務(wù),如播放音頻,視頻等為了使用MediaPlayer,我們要調(diào)用這個類的靜態(tài)create()方法
    2023-04-04

最新評論