此案例为使用SharedPreferences 轻量级的存储类 ,在登陆页保存用户名密码进行跳转并将用户名密码显示在另一个Activity的Textview上。
SharedPreferences可以保存常规的Long、Int、String等类型数据
登陆页的layout布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/user_name" android:layout_width="match_parent" android:layout_height="60dp" /> <EditText android:id="@+id/user_password" android:layout_width="match_parent" android:layout_height="60dp" /> <Button android:id="@+id/login" android:gravity="center" android:text="登陆" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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="com.example.administrator.weektext.MainActivity"> <TextView android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:background="@android:color/holo_blue_dark" android:id="@+id/text_name" android:text="衣物同" android:layout_width="match_parent" android:layout_height="50dp" /> <TextView android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:background="@android:color/holo_blue_dark" android:id="@+id/text_password" android:text="衣物同" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout>
登陆页中使用SharePrefences
package com.example.administrator.weektext; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; //首先是登陆页 public class LoginActivity extends AppCompatActivity { private EditText name,password; private Button login; private String text_name,text_password; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //自定义的初始化的方法 initView(); //自定义的按钮的点击事件方法 onclick(); } private void onclick() { //按钮的点击事件 login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //获取控件EditText中用户名和密码 text_name=name.getText().toString(); text_password=password.getText().toString(); //判断用户名和密码是否有数据 if (text_name.isEmpty()||text_password.isEmpty()){ Toast.makeText(LoginActivity.this,"用户名或密码为空",Toast.LENGTH_SHORT).show(); }else { //得到SharedPreferences对象,调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象 SharedPreferences sharedPreferences=getSharedPreferences("user",MODE_PRIVATE); SharedPreferences.Editor edit = sharedPreferences.edit(); //使用put方法将数据存入Shareprences中 edit.putString("name",text_name); edit.putString("password",text_password); edit.commit(); Intent intent=new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); } } }); } private void initView() { name = (EditText) findViewById(R.id.user_name); password = (EditText) findViewById(R.id.user_password); login = (Button) findViewById(R.id.login); } }
跳转到另一个Activity并将SharePrefences中数据拿来并在本Activity中显示
package com.example.administrator.weektext; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView text,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initview(); } private void initview() { text = (TextView) findViewById(R.id.text_name); password = (TextView) findViewById(R.id.text_password); SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE); text.setText(user.getString("name",null)); password.setText(user.getString("password",null)); } }