暑期花了一周的时间选择性的看完了第一行代码,整理一些基本的知识点,然后对一些感觉比较重要的知识点进行概括,方便自己在很长一段时间不接触后再次接触时快速回忆。
活动
、 指用户可见的,并可用于交互 (前端界面)
活动的使用
1.活动添加后要在AndroidMainifest中注册活动(as会自动生成)
2. 在activity标签内设置主活动
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
通过label 属性设置活动标题
3.修改完布局文件后,通过setContentView()方法添加布局
4.通过Toast进行提示 Toast.makeText(this,"text",时长).show()
5.使用Menu
新建Menu resourcefile
<item android:id="@+id/remove_item" android:title="Remove"/>
<item android:id="@+id/add_item" android:title="Add"/> 通过item 添加分类
修改完布局后 在活动中调用
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true; // true 显示 false 不显示
}
重写(例)
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
6.使用intent
可用于启动活动、服务、发送广播 相当于一个传递媒介,携带指定参数打到指定的效果。
显示Intent(明确给出目标组件信息) 可以把参数写完善达到精准调用(比如调用拨号时指定号码)
显示的构造函数Intent(Context packageContext,Class<?>cls)
第一个参数为内容的上下文,第二个参数为目标活动/服务
隐式Intent(未明确给出,根据配置文件匹配)
隐式的调用方法要配置AndroidMainifest.xml 通过action标签指定可以响应的action
通过category过滤附加信息。具体的以后碰到再完善。
用法: Intent intent =new Intent(this,next.class);
String data ="hello"
intent.setData(uri.parse("") ;
intent.putExtra("key",data) //向下一个活动传递参数 第一个为键 第二个为值
startActivity(intent);
String data_gets=intent.getStringExtra("key"); 取
向上一个活动传递数据
启动活动的时候使用
startActivityForResult(intent,requestcode);//第二个参数用于回调时判断数据来源
回调的活动中使用setResult(code,intent); code: (RESULT_OK/RESULT_CANCELED)
要实现通过back键返回时 要重写onBackPressed()方法
7.活动的生存周期:
Oncreate()和onDestory() 创建和销毁时调用
onStart()和onStop() 活动可见和不可见状态转变时调用
onResume()和onPause() 活动准备好处于交互状态和启动其它活动时
onRestart() 重启
8.面对 活动回收后再次使用时会调用onCreate()为了使用之前的信息
可以使用onSaveInstanceState()来保存 (存与取)
该方法在活动回收之前一定会被调用
9.活动的四种启动模式
可直接参考
https://blog.youkuaiyun.com/bobo89455100/article/details/53708193
第一行代码中关于活动的几个实用的实例
直接参考https://www.cnblogs.com/loufangcheng/p/9971396.html