Android学习-认识Activity
来源:互联网 发布:淘宝上代购药品 编辑:程序博客网 时间:2024/06/08 00:32
重新认识Activity
1>什么是Activty
Activity是一个应用程序组件,提供用户与程序交互的界面
2>Android四大组件
–Activity
–Service
–BroadcastReceiver
–Content Provider
3>Activity如何创建使用
—继承Android的Activity类
—重写方法
—设置显示布局
—在AndroidManifest文件中,注册Activity
<activity android:name=".MainActivity"><!-有name这一行就ok--> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
Activity生命周期
1>概述
onCreat();创建
onStart();运行
onResume();获取焦点
onPause();失去焦点
onStop();暂停
onDestroy();销毁
onRestart();重启
2>Activity四种状态
–活动状态 :Activity处于界面最顶端,获取焦点
–暂停状态:Activity失去焦点,但是对用户可见
–停止状态:被完全遮挡,但保留所有状态和成员信息
–非活动状态:Activity被停止
3>实验
运行程序–>onCreate: onStart: onResume:
返回屏幕–>onCreate: onStart: onResume: onPause: onStop:
点击第一个活动的按钮跳转到第二个活动(第二个活动为Dialog,因为这样没有被完全遮挡)–> onCreate: onStart: onResume: onPause:
注册活动并且设置为Dialog(这样就可以未全部覆盖)
<activity android:name=".FirstActivity" android:theme="@android:style/Theme.DeviceDefault.Dialog"/>
代码
MainActivity.javapublic class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); Log.i(TAG, "onCreate: "); Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent intent = new Intent(MainActivity.this,FirstActivity.class); MainActivity.this.startActivity(intent); } }); } protected void onStart() { super.onStart(); Log.i(TAG, "onStart: "); } protected void onRestart() { super.onRestart(); Log.i(TAG, "onRestart: "); } protected void onResume() { super.onResume(); Log.i(TAG, "onResume: "); } protected void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy: "); } protected void onStop() { super.onStop(); Log.i(TAG, "onStop: "); } protected void onPause() { super.onPause(); Log.i(TAG, "onPause: "); }}
FirstActivity.javapublic class FirstActivity extends AppCompatActivity { protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); }}
AndroidManifest.xml<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.angel.applacation"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".FirstActivity" android:theme="@android:style/Theme.DeviceDefault.Dialog"/> </application></manifest>
fisrst_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二个活动创建成功" /></LinearLayout>
main_layout.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /></LinearLayout>
阅读全文
0 0
- Android学习-认识Activity
- 【幻化万千戏红尘】qianfeng-Android-Day04-Spinner、ScrollVie、AutoCompleteTextView,Activity初步认识基础学习:
- Android NDK 学习认识
- 重新认识Android Activity的生命周期
- 初识android开发,activity的认识
- 认识 Android 中的 Activity 组件 总结
- 《android studio 1》认识Activity组件
- Android从零开始之重新认识Activity--基础知识
- Android从零开始之重新认识Activity--生命周期
- Android从零开始之认识Activity基础知识
- android自定义view系列:认识activity结构
- 认识Activity
- Android学习(3):认识APK
- [学习笔记]认识Android Service
- Android之Activity学习
- Android学习--Activity组件
- Android学习笔记----Activity
- Android学习--Activity
- 旧题再做【bzoj2287】【[pojchallenge]消失之物】分治背包
- Java随记(二)上转型对象
- hibernate之注解
- Python程序设计—车万翔
- 全排列算法【非递归活动数实现】
- Android学习-认识Activity
- 课本简单程序执行
- 责任链模式
- JQuery Ajax 参数含有特殊字符
- 桌面许多快捷方式图标/文件左下角出现绿色对号小图标
- 学习机器学习前的准备
- 编译器小知识总结
- Sublime Text 3 c语言配置
- matlab 安装 cvx 和 mosek 以及 gurobi