一、综述
觉得有必要把之前学过的android知识重新复习一下,都快忘得差不多了。
将以程序和代码为主,以知识点的形式写的Demo。知识点将在标题写出,方便查找。
二、程序代码
主Activity
//ActivityMain.java package com.fsy; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; /** * 关于Activity生命周期的文档很详细,不清楚了就查 * 多个Activity会被放入栈式结构task中,手机显示栈中最顶部的元素 * 只要没销毁就会在栈中等待出栈 * 只要启动一个新的Activity就会调用OnPause()方法 * 第二个Activity如果把第一个Activity完全遮挡住了就会调用OnStop()方法。没有则不会调用 * 对话框风格的Activity在Manifest.xml中加入一个Activity属性即可 * android:theme="@android:style/Theme.Dialog" * Menu的创建重写onCreateOptionsMenu,响应要重写onOptionsItemSelected * @author fsy * */ public class ActivityMain extends Activity { public static final int MENU_1 = 0; public static final int MENU_2 = 1; public static final int MENU_EXIT = 2; public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_1, 1, "1"); menu.add(0, MENU_2, 3, "2"); menu.add(2, MENU_EXIT, 2, "EXIT"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId() == MENU_EXIT){ finish(); } return super.onOptionsItemSelected(item); } /** Called when the activity is first created. */ Button b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); b = (Button)findViewById(R.id.button1); b.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { //Activity2为对话框风格,不会完全遮挡屏幕,所以在子进程初始化后不会再调用OnStop() Intent t = new Intent(); t.setClass(ActivityMain.this, Activity2.class); ActivityMain.this.startActivity(t); }}); } }
对话框Activity
//Activity2.java package com.fsy; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class Activity2 extends Activity{ /** Called when the activity is first created. */ Button b; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mgs); b= (Button)findViewById(R.id.button2); b.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { finish(); //结束Activity。调用其OnDestroy()方法 }}); } }
主Activity的xml
//main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="开始进程" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="fill_parent"></Button> </LinearLayout>
对话框Activity的xml
//mgs.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_height="wrap_content" android:text="点我返回" android:layout_width="fill_parent"> </TextView> <Button android:layout_height="wrap_content" android:id="@+id/button2" android:text="返回" android:layout_width="fill_parent"> </Button> </LinearLayout>
Manifest
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fsy" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".ActivityMain" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="Activity2" android:theme="@android:style/Theme.Dialog"> //设置对话框风格的activity </activity> </application> </manifest>