<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.duanlian.sharedpreferencesdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="用户名" android:textSize="23sp" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="密 码" android:textSize="23sp" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <Button android:onClick="save" android:layout_width="match_parent" android:layout_height="50dp" android:text="保存信息"/> <Button android:onClick="change" android:layout_width="match_parent" android:layout_height="50dp" android:text="跳转"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.duanlian.sharedpreferencesdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="用户名" android:textSize="23sp" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="密 码" android:textSize="23sp" /> <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> <Button android:onClick="get" android:layout_width="match_parent" android:layout_height="50dp" android:text="得到信息"/> </LinearLayout>
package com.duanlian.sharedpreferencesdemo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText passWors;
private EditText userName;
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
userName = (EditText) findViewById(R.id.username);
passWors = (EditText) findViewById(R.id.password);
//1,实例化SharedPreferences对象,参数1:保存的名字,参数2:保存的模式MODE_PRIVATE私有的
preferences = getSharedPreferences("config", MODE_PRIVATE);
//2,让SharedPreferences处于可编辑状态
editor = preferences.edit();
}
/**
* 保存按钮的监听
* @param view
*/
public void save(View view) {
//拿到用户输入的数据
String name = userName.getText().toString().trim();
String pwd = passWors.getText().toString().trim();
//3,储存数据,类似于map
editor.putString("name", name);
editor.putString("pwd", pwd);
//4,提交
editor.commit();
}
/**
* 跳转的点击监听
* @param view
*/
public void change(View view) {
Intent intent = new Intent(MainActivity.this, ActivityA.class);
startActivity(intent);
}
}
package com.duanlian.sharedpreferencesdemo;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class ActivityA extends AppCompatActivity {
private EditText userName;
private EditText passWord;
private String name;
private String pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
//得到数据
//1,得到SharedPreferences对象
SharedPreferences preferences = getSharedPreferences("config", 0);
//2,取出数据
name = preferences.getString("name","");
pwd = preferences.getString("pwd", "");
userName = (EditText) findViewById(R.id.username);
passWord = (EditText) findViewById(R.id.password);
}
/**
* 得到数据按钮的监听
* @param view
*/
public void get(View view) {
userName.setText(name);
passWord.setText(pwd);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有