实验九 使用SharedPreferences存储简单数据 一、实验要求和目的 Mainactivity SetActivity
二、实验环境
5.<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:text="欢迎来到我的家园" android:textSize="18sp" ></TextView> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt" android:textSize="18sp" android:layout_below="@+id/tv" android:text="参数设置" android:layout_marginTop="15dp" android:layout_marginLeft="100dp" ></Button> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SetActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25sp" android:text="请输入用户名:" android:id="@+id/tv2" ></TextView> <EditText android:layout_width="100dp" android:layout_height="50dp" android:id="@+id/et" android:layout_below="@+id/tv2" android:layout_marginLeft="100dp" android:layout_marginTop="35dp" ></EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et" android:layout_marginLeft="100dp" android:layout_marginTop="15dp" android:text="确定" android:textSize="20sp" android:id="@+id/bt1" ></Button> </RelativeLayout>
package com.example.shiyan9; 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); //为“参数设置”按钮绑定监听器 Button btn_set = findViewById(R.id.bt); btn_set.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it = new Intent(MainActivity.this, SetActivity.class); startActivity(it); } }); } @Override protected void onStart() { super.onStart(); preferences = getSharedPreferences("set", Context.MODE_PRIVATE); String user = preferences.getString("user", ""); TextView tv_welcome = findViewById(R.id.tv); tv_welcome.setText("欢迎 " + user + " 来到我的家园"); } }
package com.example.shiyan9; 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 SetActivity extends AppCompatActivity { private SharedPreferences preferences = null; private SharedPreferences.Editor editor = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_set); //利用SharedPreferences读取数据并显示 preferences = getSharedPreferences("set", Context.MODE_PRIVATE); //获取haredPreferences.Editor对象,尝试写数据 editor = preferences.edit(); //为“确定”按钮绑定监听器 Button btn_ok = findViewById(R.id.bt1); final EditText et_user = findViewById(R.id.et); btn_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String user = et_user.getText().toString(); editor.putString("user", user); editor.apply(); finish(); } }); } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算