您当前的位置:首页 > Android教程

Android开发学习教程(22)- Intent传值与startActivityForResult返回值

时间:2022-01-18 07:45:02 阅读数:18,687人阅读
版权声明:转载请注明出处,谢谢!
—— 你要悄悄拔尖,然后一鸣惊人。

上一篇我们讲了Intent的基本概念,现在我们通过一个实例来验证。

Intent传值

有这么一个场景,用户在登录页面输入用户名之后点击登录,跳转到另一个页面显示用户名名称。这个场景就是典型的Activity间Intent传值。下面我们开始编写代码:

	public class LoginActivity extends AppCompatActivity {

		private EditText etUsername;
		private Button btnLogin;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_login);
			initView();
		}

		private void initView() {
			etUsername = findViewById(R.id.et_username);
			btnLogin = findViewById(R.id.btn_login);
			btnLogin.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(LoginActivity.this, MainActivity.class);
					intent.putExtra("username", etUsername.getText().toString());
					startActivity(intent);
				}
			});
		}

	}
	public class MainActivity extends AppCompatActivity {

		private TextView tvUsername;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_main);
			initView();
		}

		private void initView() {
			tvUsername = findViewById(R.id.tv_username);
			tvUsername.setText("欢迎您 " + getIntent().getStringExtra("username"));
		}

	}

在登录界面输入用户名之后,点击登录,通过startActivity跳转到主页面MainActivity,在MainActivity上通过getIntent()获取Intent传值。

Intent返回值给上一个Activity

另外还有这么一个场景,比如饿了么APP用户在首页选择用户所在城市,比如选择广州之后返回,首页显示广州了。这个场景就是典型Intent返回值给上一个Activity。下面我们开始编写代码:

	public class ChooseActivity extends AppCompatActivity {

		private TextView tvCity;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_choose);
			tvCity = findViewById(R.id.tv_city);
			tvCity.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					startActivityForResult(new Intent(ChooseActivity.this, CityListViewActivity.class), 101);
				}
			});
		}

		@Override
		protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
			super.onActivityResult(requestCode, resultCode, data);
			if (requestCode == 101 && resultCode == RESULT_OK && data != null) {
				tvCity.setText(data.getStringExtra("city"));
			}
		}

	}
	public class CityListViewActivity extends AppCompatActivity {

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.activity_city);
			ListView listView = findViewById(R.id.listview);
			String[] data = {"北京", "上海", "广东", "江西", "福建", "江苏", "山东", "浙江", "四川", "云南", "贵州", "黑龙江", "哈尔滨"};
			ArrayAdapter<String> array = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
			listView.setAdapter(array);
			listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					Intent intent = new Intent();
					intent.putExtra("city", data[position]);
					setResult(RESULT_OK, intent);
					finish();
				}
			});
		}

	}

完整项目源码地址:https://github.com/heluicare/some-note

转载请注明出处,谢谢!

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"