亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

本文實(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>

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

相關(guān)文章

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

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

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

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

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

    android 應(yīng)用退出時(shí)不播放動(dòng)畫(huà)的解決方法

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

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

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

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

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

    Android仿高德首頁(yè)三段式滑動(dòng)效果的示例代碼

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

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

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

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

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

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

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

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

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

最新評(píng)論