无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

AndroidStudio存數(shù)據(jù)與提取數(shù)據(jù)

2022-11-16 12:34 作者:huawei13Pro  | 我要投稿

來源:我的學(xué)習(xí)筆記

1.新建一個項(xiàng)目

新建項(xiàng)目

2.找到空白的activity點(diǎn)擊next

點(diǎn)擊next


3.起一個名字

點(diǎn)擊next

4.再編輯xml頁面

編輯頁面

5.編輯XXX.java頁面

編輯


具體代碼如下:

package com.自己創(chuàng)建的包的名字;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.nio.Buffer;

public class MainActivity extends AppCompatActivity {
? ? ? Button btn1;
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?btn1=findViewById(R.id.button1);



? ? ? ?//sharedprefences存數(shù)據(jù)
? ? ? ?SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
? ? ? ?//1.獲取sp對象,通過getSharedPrefences(文件名,文件操作模式);
? ? ? ?//本代碼中,數(shù)據(jù)會保存再指定目錄下的data.xml中
? ? ? ?SharedPreferences.Editor editor=sp.edit();
? ? ? ?//調(diào)用sp的edit()方法獲取Edit編輯器對象,通過編輯器才能存數(shù)據(jù)
? ? ? ?editor.putString("name","拼多多");
? ? ? ?editor.putInt("tel",1008611);

? ? ? ?//3.通過編輯器,調(diào)用put+數(shù)據(jù)類型(關(guān)鍵詞,數(shù)據(jù));

? ? ? ?editor.apply();
? ? ? ?//4.用commit()或者apply()方法提交數(shù)據(jù)


? ? ? ?btn1.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View view) {
? ? ? ? ? ? ? ?SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
? ? ? ? ? ? ? ?//創(chuàng)建sp對象,指定從那個文件中的數(shù)據(jù)
? ? ? ? ? ? ? ?String n=sp.getString("name","無數(shù)據(jù)");
? ? ? ? ? ? ? ?int a=sp.getInt("tel",0);
? ? ? ? ? ? ? ?//2.調(diào)用sp的get+數(shù)據(jù)類型(關(guān)鍵詞,找不到該關(guān)鍵詞數(shù)據(jù)時返回的內(nèi)容)
? ? ? ? ? ? ? ?Toast.makeText(MainActivity.this,n+a,Toast.LENGTH_LONG).show();


? ? ? ? ? ?}
? ? ? ?});









? ?}
}


出現(xiàn)的效果如下:

點(diǎn)開看看新增的數(shù)據(jù)

虛擬機(jī)運(yùn)行效果


AndroidStudio存數(shù)據(jù)與提取數(shù)據(jù)的評論 (共 條)

分享到微博請遵守國家法律
宁远县| 新邵县| 浪卡子县| 安远县| 高清| 克拉玛依市| 金塔县| 商洛市| 青河县| 拜城县| 乃东县| 怀化市| 视频| 岫岩| 英超| 南溪县| 卢氏县| 会东县| 临清市| 义乌市| 三江| 长岭县| 陕西省| 噶尔县| 凌源市| 高雄县| 夏津县| 桂平市| 巴彦淖尔市| 岑巩县| 武宁县| 安西县| 黄大仙区| 眉山市| 陈巴尔虎旗| 南华县| 乌拉特前旗| 浦县| 且末县| 海伦市| 宜城市|