Android_檔案儲存體

來源:互聯網
上載者:User

標籤:android   style   blog   ar   使用   java   sp   檔案   資料   

  因為項目的需求,想實現一個指導使用的歡迎頁面效果,通過在網上的詢問,給的一種解決辦法是通過SharedPreferences檔案儲存體方式來實現,具體的實作類別似於通過第一次取得SharedPreferences中的資料時,如果資料不存在時,將返回預設的值,具體的代碼實現,馬上奉上。

MainActivity的代碼:

public class MainActivity extends Activity {public boolean flag = false;public TextView textview;public String FILE_NAME="SharedPreferences";//儲存資料的唯一標識@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textview = (TextView)findViewById(R.id.textview);init();}public void init(){/* * Android中的檔案儲存體,當第一次取資料時如果還沒有存入資料,將返回預設資料 */SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);//從檔案中擷取儲存的資料String usernameContent = sharedPreferences.getString("username", "預設資料");textview.setText(usernameContent);/*SharedPreferences sp = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);Editor editor = sp.edit();editor.clear();editor.putString("name", "正常資料");editor.commit();String name = sp.getString("name", "預設資料");textview.setText(name);*/}}

   相信通過這個代碼,一定能讓你對SharedPreferences儲存有一個詳細的認識。

              ------------如有錯誤,還望多多指點-------------

Android_檔案儲存體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.