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

標題: android SharedPreferences存儲數據 [打印本頁]

作者: 2313536951    時間: 2020-12-17 21:09
標題: android SharedPreferences存儲數據
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>






歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 亚洲精品一区二区三区蜜桃久 | 日日噜 | 久久国产秒 | 久久99精品久久久久久狂牛 | 日本亚洲精品成人欧美一区 | 日韩精品一区二区三区 | 久久精品久久精品久久精品 | 91在线播 | 免费成人av网站 | 五月综合激情婷婷 | 国产视频二区 | 少妇一区在线观看 | 日韩视频一区二区在线 | 欧美性影院| 亚洲欧美日韩网站 | 在线视频中文字幕 | 91精品国产日韩91久久久久久 | 精品久久久久久亚洲精品 | 日本高清视频在线播放 | 国产免费观看久久黄av片涩av | 色综合99 | 国产女人与拘做受视频 | 欧美视频福利 | 中文字幕在线精品 | 九九亚洲| 成人h动漫精品一区二区器材 | 中文在线一区二区 | 野狼在线社区2017入口 | 国产做爰 | 香蕉视频91 | 久久亚洲综合 | 欧美极品视频在线观看 | 国产精品福利网站 | 黄色在线免费观看视频 | 亚洲视频在线观看 | 久久国产精品一区二区三区 | 在线播放国产视频 | 欧美精品91| 黄色av网站免费看 | 91在线最新 | 91精品国产综合久久精品 |