Android之Activity
来源:互联网 发布:智多星软件多少钱 编辑:程序博客网 时间:2024/05/16 14:49
一.初步认识
src
package cn.android.lyj;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class TestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button)findViewById(R.id.bt1); button.setOnClickListener(new OnClickListener() {public void onClick(View v) {TextView textView = (TextView)findViewById(R.id.tv1);textView.setText("修改后");}}); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bt_text" /></LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">修改前text</string> <string name="app_name">Test</string> <string name="bt_text">修改text</string></resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.android.lyj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".TestActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
二.生命周期
注:缺少OnRestart(), 在OnStop()执行后当重新打开这个Activity时会被调用,OnRestart()——>onStart()——>onResume()
src
package cn.android.lyj;import android.app.Activity;import android.os.Bundle;import android.util.Log;public class TestActivity extends Activity {private static final String TAG = "LYJ"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(TAG, "onCreate()"); } @Overrideprotected void onStart() {// TODO Auto-generated method stubsuper.onStart();Log.i(TAG, "onStart()");}@Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();Log.i(TAG, "onResume()");}@Overrideprotected void onPause() {// TODO Auto-generated method stubsuper.onPause();Log.i(TAG, "onPause()");}@Overrideprotected void onStop() {// TODO Auto-generated method stubsuper.onStop();Log.i(TAG, "onStop()");}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();Log.i(TAG, "onDestroy()");}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">测试生命周期</string> <string name="app_name">Test</string> </resources>
AndroidManifest.xml同上
二.运行结果:
启动运行:
分两种情况:
1.点击模拟器返回按钮——>会destory这个Activity
2.点击模拟器主页按钮——>用栈保存这个Activity,下次重新打开时不会再create
下次再打开这个Activity
三.细节
1. it is popped from the stack (and destroyed) and the previous activity resumes.
2. The two most important callback methods are.
a. onCreate() : You must implement this method. The system calls this when creating your activity.
b. onPause() : The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
3. ViewGroup继承自View.
4. An intent can also carry small amounts of data to be used by the activity that is started.
- Android之activity
- Android之Activity 2
- Android 之 Activity 生命周期
- android之activity生命周期
- Android 之 Activity 生命周期
- android 之 Task Activity
- Android之Activity学习
- android组件之Activity
- 精通android之Activity
- Android之Activity
- android 之 Activity lifecycle
- 05.Android之Activity
- Android之Activity
- Android入门之Activity
- Android之Activity组件
- android组件之Activity
- Android之Activity
- Android之Task Activity
- java判断字符串内容类型
- (续)flex读取本地文件XML
- 爱我的人和我爱的人
- IIS优化
- 1042 N!(java)
- Android之Activity
- Linux下chkconfig命令详解
- JSP标签与web开发的4个域对象
- 购物车(一)
- JSP中九大隐式对象
- 二分图的最小点覆盖跟最小点权覆盖
- AR
- Asp.net控制TextBox只能输入数字
- ASP.NET中Server.MapPath() 和Request.MapPath()使用