不同活动页面传值
使用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;
public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
@Override protected void onStart() { super.onStart(); }
@Override protected void onResume() { super.onResume(); }
@Override protected void onPause() { super.onPause(); }
@Override protected void onStop() { super.onStop(); }
@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)); } } }
|
生命周期顺序示例

页面传值回之前页面
父页面
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; } }
@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 共享参数
保存一些键值对到本地(手机内存当中), 如果有需要使用,可以调用
创建对象 SharedPreferences preferences
preferences = getSharedPreferences("user_pref", MODE_PRIVATE);
在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); } }
|
使用 SharedPreferences.Editor editor = preferences.edit();
进行存储
1 2 3 4
| editor.putString("uname", uname); editor.putString("upwd", upwd); editor.putInt("uImg", iconId); editor.commit();
|
使用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);
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; }
@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; } } }
|