完成如下程序(主Activity界面如图1所示):
在图1中点击“参数设置”按钮,启动如图2所示的Activity。在图2中用户输入完用户名后,点击“确定”按钮,将关闭该Activity(此时需要使用SharedPreferences.Editor
保存用户名),主Activity重新回到运行状态,如图3所示(需重写主Activity的生命周期回调方法onStart()或onResume(),完成对SharedPreferences中保存的用户名的读取)。
布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="216dp"
android:text="欢迎来到我的家园"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="参数设置"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_welcome" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_new.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".NewActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="208dp"
android:text="请输入用户名:"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_name"
android:layout_width="198dp"
android:layout_height="53dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="确定"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.test9; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; 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 Activity { private SharedPreferences preferences; private TextView tv_welcome; private Button btn_set; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_welcome=findViewById(R.id.tv_welcome); preferences= this.getSharedPreferences("usersetting",MODE_PRIVATE); tv_welcome.setText("欢迎"+preferences.getString("username"," ")+"来到我的家园"); 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,NewActivity.class); //启动指定Activity并等待返回的结果,其中0是请求码,用于标识该请求。 startActivity(intent); MainActivity.this.finish(); } }); } }
NewActivity.java
package com.example.test9; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class NewActivity extends Activity { private SharedPreferences preferences; private SharedPreferences.Editor editor; private Button btn_submit; private EditText et_name; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); et_name=findViewById(R.id.et_name); btn_submit=findViewById(R.id.btn_submit); btn_submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { preferences=getSharedPreferences("usersetting",MODE_PRIVATE); editor=preferences.edit(); //将输入的数据写入SharedPreference editor.putString("username",et_name.getText().toString()); editor.commit(); Intent intent=new Intent(NewActivity.this,MainActivity.class); startActivity(intent); NewActivity.this.finish(); } }); } }
标签:存储,private,SharedPreferences,Activity,import,Android,btn,android From: https://www.cnblogs.com/soaring27221/p/16943367.html