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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3169|回復: 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 共享資料的黑幣獎勵!

查看全部評分

回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久久亚洲 | 国产一级片一区二区 | 国产精品免费看 | 曰韩三级 | 中文字幕一区二区三区乱码在线 | 特黄色一级毛片 | 一区二区三区四区在线 | 日韩成人精品 | 欧美最猛黑人xxxx黑人 | 亚洲精品久久久蜜桃 | 夜夜爽夜夜操 | 欧美一区二区三区在线免费观看 | a级在线| 狠狠婷婷综合久久久久久妖精 | 91人人看 | 亚洲成人一区 | 在线观看毛片网站 | 国内精品久久久久 | 亚洲最大成人综合 | 好好的日在线视频 | 精品一区二区三区入口 | 成人在线视| 中文字幕综合 | 青春草91 | 毛片在线免费 | 少妇性l交大片免费一 | www.日韩系列 | 热99精品视频 | 手机看片在线播放 | com.色.www在线观看 | 亚洲成a人片 | 欧美精品一区二区三 | 黄色片免费看视频 | 欧美久久精品 | 蜜臀久久| 久久久久国产 | 亚洲综合热| 亚洲综合一区二区三区 | 18性欧美 | 久久国产精品99久久久久 | 色爱综合网 |