久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3155|回復: 0
打印 上一主題 下一主題
收起左側

android SharedPreferences存儲數據

[復制鏈接]
跳轉到指定樓層
樓主
ID:435645 發表于 2020-12-17 21:09 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
MainActivity.java:
package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加載布局文件
        //獲取ui控件
        Button btn_set=findViewById(R.id.btn_set);
        //按鈕點擊事件
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //創建意圖
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);//開啟意圖
            }
        });
    }
    //重寫onStart()函數
    @Override
    protected void onStart() {
        super.onStart();//繼承
        //指定該SharedPreferences數據只能被本應用程序讀寫
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //讀取字符數據
        String user = preferences.getString("user", "");
        //獲取textview控件
        TextView tx = findViewById(R.id.tx);
        //設置顯示內容
        tx.setText("歡迎 " + user + " 來到我的家園");
    }
}package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences preferences = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//加載布局文件
        //獲取ui控件
        Button btn_set=findViewById(R.id.btn_set);
        //按鈕點擊事件
        btn_set.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //創建意圖
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(intent);//開啟意圖
            }
        });
    }
    //重寫onStart()函數
    @Override
    protected void onStart() {
        super.onStart();//繼承
        //指定該SharedPreferences數據只能被本應用程序讀寫
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //讀取字符數據
        String user = preferences.getString("user", "");
        //獲取textview控件
        TextView tx = findViewById(R.id.tx);
        //設置顯示內容
        tx.setText("歡迎 " + user + " 來到我的家園");
    }
}
MainActivity2.java:
package com.example.android9;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity2 extends AppCompatActivity {
    private SharedPreferences preferences = null;
    private SharedPreferences.Editor editor = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加載布局文件
        setContentView(R.layout.activity_main2);
        //利用SharedPreferences讀取數據并顯示
        preferences = getSharedPreferences("crazyit", Context.MODE_PRIVATE);
        //獲取haredPreferences.Editor對象,嘗試寫數據
        editor = preferences.edit();
        //為“確定”按鈕綁定監聽器
        Button btn_ok = findViewById(R.id.btn);
        final EditText tv = findViewById(R.id.tv);
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String user = tv.getText().toString();
                editor.putString("user", user);
                editor.apply();
                finish();
            }
        });
    }
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="43dp"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="31dp"
        android:textSize="22dp"
        android:text="歡迎  來到我的家園" />
    <Button
        android:id="@+id/btn_set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tx"
        android:layout_marginLeft="130dp"
        android:textSize="22dp"
        android:text="參數設置"/>
</RelativeLayout>
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity2">
    <TextView
        android:id="@+id/tx2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入用戶名:"
        android:textSize="28dp" />

    <EditText
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tx2"
        android:inputType="text"
        android:ems="9"
        android:textColorHighlight="@color/colorAccent"
        android:textSize="28dp" />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv"
        android:textSize="28dp"
        android:layout_marginLeft="100dp"
        android:text="確定"/>

</RelativeLayout>

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 99精品久久久久久中文字幕 | 成人激情视频 | 嫩草研究影院 | 午夜精品一区二区三区在线视频 | 国产在线色| 欧美一级大片免费观看 | 日韩中文一区二区三区 | 国产丝袜一区二区三区免费视频 | 欧美精品区 | 日韩三极 | 日本三级播放 | 成人av久久 | 犬夜叉在线观看 | 欧美日韩一区二区三区不卡视频 | 黄色一级大片在线免费看产 | 婷婷亚洲综合 | 中文字幕一区二区三区四区五区 | 久久国产高清 | 日韩av手机在线观看 | 国产乱码精品一品二品 | 一区二区三区不卡视频 | 亚洲视频在线看 | 日韩免费中文字幕 | 午夜小视频在线观看 | 亚洲一区日韩 | 蜜臀网 | 日本在线精品视频 | 亚洲精品电影在线 | 在线国产一区 | 免费国产精品久久久久久 | 成人免费网视频 | 超碰av在线 | 国产成人精品久久二区二区91 | 日韩欧美在线播放 | 自拍偷拍欧美 | 亚洲网一区 | 欧美日韩在线免费观看 | 97日日碰人人模人人澡分享吧 | 成人3d动漫一区二区三区91 | 色综合色综合色综合 | 在线观看国产三级 |