这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 | |||
|
wiki:book:notes:android:第一行代码-android-第2版-郭霖:第2章-先从看得到的入手-探究活动 [2022/04/06 13:23] 博丽幻月 移除 |
— (当前版本) | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | ====== 第2章 先从看得到的入手——探究活动 ====== | + | |
| - | **活动Activity**\\ | + | |
| - | 主要用于和用户进行交互。\\ | + | |
| - | ---- | + | |
| - | **启动的主活动**\\ | + | |
| - | 在''< | + | |
| - | <code xml> | + | |
| - | <action android: | + | |
| - | < | + | |
| - | </ | + | |
| - | ---- | + | |
| - | **通知消息Toast**\\ | + | |
| - | 用途:将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间。\\ | + | |
| - | <code java> | + | |
| - | Button button1 = (Button) findViewByid(R.id.button_1); | + | |
| - | button1.setOnClicklistener(new View.OnClicklistener() { | + | |
| - | @Override | + | |
| - | public void onClick(View v) { | + | |
| - | Toast.makeText(FirstActivity.this, | + | |
| - | } | + | |
| - | }); | + | |
| - | </ | + | |
| - | 可以通过'' | + | |
| - | '' | + | |
| - | ---- | + | |
| - | **活动中使用Menu**\\ | + | |
| - | '' | + | |
| - | ---- | + | |
| - | **销毁一个活动**\\ | + | |
| - | '' | + | |
| - | ---- | + | |
| - | **Intent交互**\\ | + | |
| - | Intent是Android程序中各组件之间**进行交互**的一种重要方式,它不仅可以指明当前组件**想要执行的动作**,还可以在不同组件之间**传递数据**。\\ | + | |
| - | Intent一般可被用于**启动活动**、**启动服务**、**发送广播**等场景。\\ | + | |
| - | Intent分两种:显示Intent、隐式Intent。\\ | + | |
| - | ---- | + | |
| - | **使用隐式Intent**\\ | + | |
| - | <code java> | + | |
| - | intent.addCategory(" | + | |
| - | </ | + | |
| - | <code xml> | + | |
| - | < | + | |
| - | < | + | |
| - | <action android: | + | |
| - | < | + | |
| - | < | + | |
| - | </ | + | |
| - | </ | + | |
| - | </ | + | |
| - | ---- | + | |
| - | **更多隐式Intent用法**\\ | + | |
| - | 不仅可以启动自己程序内的活动,还可以启动其他程序的活动。\\ | + | |
| - | <code java> | + | |
| - | // | + | |
| - | Intent intent = new Intent(Intent.ACTION_VIEW); | + | |
| - | intent.setData(Uri.parse(" | + | |
| - | </ | + | |
| - | <code java> | + | |
| - | // | + | |
| - | Intent intent = new Intent(Intent.ACTION_DIAL); | + | |
| - | intent.setData(Uri.parse(" | + | |
| - | </ | + | |
| - | ---- | + | |
| - | **向下一个活动传递数据**\\ | + | |
| - | 使用'' | + | |
| - | <code java> | + | |
| - | String data = "Hello SecondActivity"; | + | |
| - | Intent intent = new Intent{FirstActivity.this, | + | |
| - | intent.putExtra(" | + | |
| - | </ | + | |
| - | 使用'' | + | |
| - | <code java> | + | |
| - | Intent intent = getIntent(); | + | |
| - | String data = intent.getStringExtra(" | + | |
| - | </ | + | |
| - | ---- | + | |
| - | **返回数据给上一个活动**\\ | + | |
| - | FirstActivity按钮onClick()中添加:\\ | + | |
| - | <code java> | + | |
| - | Intent intent = new Intent(FirstActivity.this, | + | |
| - | startActivityforResult(intent, | + | |
| - | </ | + | |
| - | '' | + | |
| - | SecondActivity按钮onClick()中添加:\\ | + | |
| - | <code java> | + | |
| - | Intent intent = new Intent(); | + | |
| - | intent.putExtra(" | + | |
| - | setResult(RESULT_OK, | + | |
| - | </ | + | |
| - | 调用了'' | + | |
| - | SecondActivity销毁后,在FirstActivity中判断:\\ | + | |
| - | <code java> | + | |
| - | @Override | + | |
| - | protected void onActivityResult(int requestCode, | + | |
| - | switch (requestCode) { | + | |
| - | case 1: | + | |
| - | if (resultCode == RESULT_OK) { | + | |
| - | String returnedData = data.getStringExtra(" | + | |
| - | Log.d(" | + | |
| - | break; | + | |
| - | default: | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | 如果使用'' | + | |
| - | ---- | + | |
| - | **TBD**\\ | + | |