不同活动页面传值

使用intent进行传值

1
2
3
4
5
Intent intent = new Intent(this, ResultActivity.class);
//传值
intent.putExtra("jia", jRes);
intent.putExtra("yi", yRes);
startActivity(intent);
1
2
3
4
//接收跳转过来页面的传值
Intent intent = getIntent();
String jia = intent.getStringExtra("jia");
String yi = intent.getStringExtra("yi");

activity

编写页面逻辑代码的类,有对应的绑定布局

生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.xyl.app1105;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

/**
* 编写页面逻辑代码的类,有对应的绑定布局
* 1. 生命周期:
* 2. 跳转页面 startActivity(intent)
* 3. 结束页面 finish()
* 4. 传值
* A--->B B---->A 将B页面返回到A页面,然后怎么传数据
*/
public class MainActivity extends AppCompatActivity {

/**
* 在创建时会被调用
* 初始化控件,初始化数据源
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

/**
* 当activity启动时会调用
*/
@Override
protected void onStart() {
super.onStart();
}

/**
* resume:获取焦点
* 获取到焦点时被调用
* 更新数据信息
*/
@Override
protected void onResume() {
super.onResume();
}

/**
* 失去焦点时被调用
*/
@Override
protected void onPause() {
super.onPause();
}

/**
* 当当前页面暂停时会被调用
*/
@Override
protected void onStop() {
super.onStop();
}

/**
* 当activity销毁时调用 finish()
*/
@Override
protected void onDestroy() {
super.onDestroy();
}

/**
* 当页面重新获取焦点时被调用
*/
@Override
protected void onRestart() {
super.onRestart();
}

public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
startActivity(new Intent(this, SecondActivity.class));
}
}
}

生命周期顺序示例

alt

页面传值回之前页面

父页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.log_iv:
Intent intent = new Intent(this, SelectIconActivity.class);
startActivityIfNeeded(intent, 1);
break;
case R.id.log_btn:
break;
}
}

/**
* 接收其他页面传回来的数值的方法 A---->B B---->A
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判断是哪个页面传回来的,根据结果码判断
if (resultCode == 100) {
//第二个参数为默认值
int resId = data.getIntExtra("resId", R.mipmap.baiyang);
iconIv.setImageResource(resId);
}
}

子页面

1
2
3
4
5
6
7
8
9
10
11
12
AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
int resId = mDatas.get(pos);
//将当前被选中的图片的地址,传到上一级
Intent intent = new Intent();
intent.putExtra("resId", resId);
//将当前页面数据传递给上一级页面的方法
setResult(100, intent);
finish();
}
};

保存数据

SharePreferences 共享参数
保存一些键值对到本地(手机内存当中), 如果有需要使用,可以调用

  1. 创建对象 SharedPreferences preferences
    preferences = getSharedPreferences("user_pref", MODE_PRIVATE);

  2. 在onCreate生命周期显示共享参数信息
    showPreferenceInfo();

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    /**
    * 显示共享参数的信息
    */
    private void showPreferenceInfo() {
    String uname = preferences.getString("uname", "");
    String upwd = preferences.getString("upwd", "");
    iconId = preferences.getInt("uImg", iconId);
    if(!TextUtils.isEmpty(uname)){
    nameEt.setText(uname);
    pwdEt.setText(upwd);
    iconIv.setImageResource(iconId);
    saveCb.setChecked(true);
    }
    }
  3. 使用 SharedPreferences.Editor editor = preferences.edit(); 进行存储

    1
    2
    3
    4
    editor.putString("uname", uname);
    editor.putString("upwd", upwd);
    editor.putInt("uImg", iconId);
    editor.commit();
  4. 使用clear()清空

    1
    2
    editor.clear();
    editor.commit();

示例代码

LoginActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.xyl.app1105;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
EditText nameEt, pwdEt;
ImageView iconIv;
CheckBox saveCb;
Button logBtn;

SharedPreferences preferences;
int iconId = R.mipmap.baiyang; //头像显示的资源

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
setEvent();
//创建共享参数对象
preferences = getSharedPreferences("user_pref", MODE_PRIVATE);
//显示共享参数的信息
showPreferenceInfo();
}

/**
* 显示共享参数的信息
*/
private void showPreferenceInfo() {
String uname = preferences.getString("uname", "");
String upwd = preferences.getString("upwd", "");
iconId = preferences.getInt("uImg", iconId);
if(!TextUtils.isEmpty(uname)){
nameEt.setText(uname);
pwdEt.setText(upwd);
iconIv.setImageResource(iconId);
saveCb.setChecked(true);
}
}

private void setEvent() {
iconIv.setOnClickListener(this);
logBtn.setOnClickListener(this);
}

private void initView() {
nameEt = findViewById(R.id.log_et_name);
pwdEt = findViewById(R.id.log_et_pwd);
iconIv = findViewById(R.id.log_iv);
saveCb = findViewById(R.id.log_cb);
logBtn = findViewById(R.id.log_btn);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.log_iv:
Intent intent = new Intent(this, SelectIconActivity.class);
// startActivityForResult(intent, 1);
startActivityIfNeeded(intent, 1);
break;
case R.id.log_btn:
//获取内容
String uname = nameEt.getText().toString().trim();
String upwd = pwdEt.getText().toString().trim();
//判断用户名和密码是否正确
boolean isSuccess = judgeInfo(uname, upwd);
if (isSuccess) {
//登录成功,判断是否保存用户信息
SharedPreferences.Editor editor = preferences.edit();
if (saveCb.isChecked()) {
//保存数据到共享参数当中
editor.putString("uname", uname);
editor.putString("upwd", upwd);
editor.putInt("uImg", iconId);
editor.commit();
return;
}
//如果选择不保存,原来的数据清空
else{
editor.clear();
editor.commit();
return;
}
}
break;
}
}

private boolean judgeInfo(String uname, String upwd) {
/**
* 省略联网判断登录的过程
*/
if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(upwd)) {
Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
return false;
}
Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
return true;
}

/**
* 接收其他页面传回来的数值的方法 A---->B B---->A
*
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//判断是哪个页面传回来的,根据结果码判断
if (resultCode == 100) {
//第二个参数为默认值
int resId = data.getIntExtra("resId", R.mipmap.baiyang);
iconIv.setImageResource(resId);
iconId = resId;
}
}
}