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:
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" >
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="欢迎 来到我的家园" />
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="参数设置"/>
activity_main2.xml:
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">
android:id="@+id/tx2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名:"
android:textSize="28dp" />
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" />
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="确定"/>