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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3055|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 免费视频成人国产精品网站 | 羞羞的视频免费观看 | 黄色毛片黄色毛片 | 久久夜视频| 日韩精品一区二区三区视频播放 | 成人免费影院 | 亚洲欧美精品久久 | 日韩网站在线观看 | 精品视频在线播放 | 国产亚洲精品久久久优势 | 欧美二区三区 | 91在线影院 | www.三级| 日韩成人在线视频 | 日韩在线播放一区 | 一区欧美 | 久久精品毛片 | 天天看天天摸天天操 | 成人高清在线 | 蜜桃视频在线观看免费视频网站www | 美女在线视频一区二区三区 | av免费网站在线观看 | 亚洲影音先锋 | 亚洲精品一区在线 | 99热热热| www久久 | 日批免费看 | 成人亚洲精品 | av国产精品| 亚洲福利视频一区二区 | www.一级片 | 国产69精品久久久久777 | 久久久久国产精品 | 国产一区二区在线视频 | 亚洲一区二区三区免费观看 | 免费a网站 | 男人的天堂久久 | 一区二区影院 | 国产精品久久久久久妇女 | 成人免费网站www网站高清 | 久久久久久久av麻豆果冻 |