Chinaunix首页 | 论坛 | 博客
  • 博客访问: 208022
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2013-12-10 10:29:53

01_04_Activity初步
1、Activity的主要作用
2、创建一个Activity的方法
3、在AndroidManifest.xml文件当中注册应用Activity的方法
4、在Activity中添加控件的方法


创建Activity的要点
        1、一个Activity就是一个类,并且这个类要继承Activity类(来源自android.jar这个包)
        2、需要复写onCreate方法(一个Activity运行时,首先调用OnCreate方法,由Android的操作系统调用的)
        3、每一个Activity必须在AndroidManifest.xml中注册并配置
        4、为Activity添加必要的控件

创建Activity的要点
        1、一个Activity就是一个类,并且这个类要继承Activity类(来源自android.jar这个包)
        2、需要复写onCreate方法(一个Activity运行时,首先调用OnCreate方法,由Android的操作系统调用的)

点击(此处)折叠或打开

  1. //Activity01.java
  2. package com.lwb.myactivity_01;

  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.view.Menu;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. /*
  9.  * 创建Activity的要点
  10.  * 1、一个Activity就是一个类,并且这个类要继承Activity类(来源自android.jar这个包)
  11.  * 2、需要复写onCreate方法(一个Activity运行时,首先调用OnCreate方法,由Android的操作系统调用的)
  12.  * 3、每一个Activity必须在AndroidManifest.xml中注册并配置
  13.  * 4、为Activity添加必要的控件
  14.  * */
  15. public class Activity01 extends Activity {

  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState); //首先,调用父类的onCreate()方法
  19.         setContentView(R.layout.activity_activity01); //设置这个Activity的布局文件为activity_activity01
  20.         TextView myTextView=(TextView)findViewById(R.id.myTextView);//通过控件的Id找到控件
  21.         Button myButton =(Button)findViewById(R.id.myButton);
  22.         myTextView.setText("我的第一个TextView");
  23.         myButton.setText("我的第一个Button"+"\n"+"test");
  24.         
  25.         
  26.     }

  27.     @Override
  28.     public boolean onCreateOptionsMenu(Menu menu) {
  29.         // Inflate the menu; this adds items to the action bar if it is present.
  30.         getMenuInflater().inflate(R.menu.activity01, menu);
  31.         return true;
  32.     }

  33. }
        3、每一个Activity必须在AndroidManifest.xml中注册并配置
       

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--AndroidManifest.xml myActivity_01 Manifest.xml -->
  3. <manifest xmlns:android=""
  4.  
  5.     package="com.lwb.myactivity_01"
  6.     android:versionCode="1"
  7.     android:versionName="1.0" >
  8.       <!--包名 package="com.lwb.myactivity_01"-->     
  9.     <uses-sdk
  10.         android:minSdkVersion="10"                
  11.         android:targetSdkVersion="17" />
  12.         <!--SDK 最低版本10即 2.3.3 -->        
  13.         <!--SDK 最高版本17 -->

  14.     <application
  15.         android:allowBackup="true"
  16.         android:icon="@drawable/ic_launcher"
  17.         android:label="@string/app_name"
  18.         android:theme="@style/AppTheme" >
  19.         <!--应用程序的图标 -->
  20.         <!--应用程序的标签 -->
  21.         <activity
  22.             android:name="com.lwb.myactivity_01.Activity01"
  23.             
  24.             android:label="@string/app_name" >
  25.              <!--Activity名 -->
  26.             <intent-filter>
  27.                 <action android:name="android.intent.action.MAIN" />
  28.                 <category android:name="android.intent.category.LAUNCHER" />
  29.             </intent-filter>
  30.              <!-- 某个应用程序中,配置了intent-filter,表这个Activity是程序中第一个运行的Activity -->
  31.         </activity>
  32.     </application>

  33. </manifest>


        4、为Activity添加必要的控件

点击(此处)折叠或打开

  1. <!-- activity_activity01.xml -->
  2.  <!-- 布局,通常一个Activity对应着一个布局,当然,可一个多个Activity使用一个布局 -->
  3. <!--
  4. 线性布局LinearLayout,android:orientation="vertival"
  5.  绝对布局AbsoluteLayout、
  6.  相对布局RelativeLayout
  7.  框架布局FranworkLayout
  8.  表格布局TableLayout-->
  9. <LinearLayout xmlns:android=""
  10.     xmlns:tools=""
  11.     
  12.     android:layout_width="match_parent"
  13.     android:layout_height="match_parent"
  14.     android:paddingBottom="@dimen/activity_vertical_margin"
  15.     android:paddingLeft="@dimen/activity_horizontal_margin"
  16.     android:paddingRight="@dimen/activity_horizontal_margin"
  17.     android:paddingTop="@dimen/activity_vertical_margin"
  18.     tools:context=".Activity01" >

  19.     <TextView
  20.         android:id="@+id/myTextView"
  21.         android:layout_width="wrap_content"
  22.         android:layout_height="wrap_content"
  23.        />
  24.     <!-- android:text="@string/hello_world" -->
  25.   
  26.     <Button
  27.         android:id="@+id/myButton"
  28.         android:layout_width="fill_parent"
  29.         android:layout_height="wrap_content"
  30.         />

  31. </LinearLayout>
R.java文件如下:

点击(此处)折叠或打开

  1. /* AUTO-GENERATED FILE. DO NOT MODIFY.
  2.  *
  3.  * This class was automatically generated by the
  4.  * aapt tool from the resource data it found. It
  5.  * should not be modified by hand.
  6.  */

  7. package com.lwb.myactivity_01;

  8. public final class R {
  9.     public static final class attr {
  10.     }
  11.     public static final class dimen {
  12.         /** Default screen margins, per the Android Design guidelines.

  13.          Customize dimensions originally defined in res/values/dimens.xml (such as
  14.          screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
  15.     
  16.          */
  17.         public static final int activity_horizontal_margin=0x7f040000;
  18.         public static final int activity_vertical_margin=0x7f040001;
  19.     }
  20.     public static final class drawable {
  21.         public static final int ic_launcher=0x7f020000;
  22.     }
  23.     public static final class id {
  24.         public static final int action_settings=0x7f080002;
  25.         public static final int myButton=0x7f080001;
  26.         public static final int myTextView=0x7f080000;
  27.     }
  28.     public static final class layout {
  29.         public static final int activity_activity01=0x7f030000;
  30.     }
  31.     public static final class menu {
  32.         public static final int activity01=0x7f070000;
  33.     }
  34.     public static final class string {
  35.         public static final int action_settings=0x7f050001;
  36.         public static final int app_name=0x7f050000;
  37.         public static final int hello_world=0x7f050002;
  38.     }
  39.     public static final class style {
  40.         /**
  41.         Base application theme, dependent on API level. This theme is replaced
  42.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
  43.     

  44.             Theme customizations available in newer API levels can go in
  45.             res/values-vXX/styles.xml, while customizations related to
  46.             backward-compatibility can go here.
  47.         

  48.         Base application theme for API 11+. This theme completely replaces
  49.         AppBaseTheme from res/values/styles.xml on API 11+ devices.
  50.     
  51.  API 11 theme customizations can go here.

  52.         Base application theme for API 14+. This theme completely replaces
  53.         AppBaseTheme from BOTH res/values/styles.xml and
  54.         res/values-v11/styles.xml on API 14+ devices.
  55.     
  56.  API 14 theme customizations can go here.
  57.          */
  58.         public static final int AppBaseTheme=0x7f060000;
  59.         /** Application theme.
  60.  All customizations that are NOT specific to a particular API-level can go here.
  61.          */
  62.         public static final int AppTheme=0x7f060001;
  63.     }
  64. }

多做些记录和多做些整理,有空多回来看看,还是有好处的,别老忘了!!!




阅读(364) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~