• Activity初步


    首先我们先创建一个Activity,所谓的Activity,就是平时WEB开发中经常接触到的页面(Page),我们可以这样来理解

    1、在src文件夹的包下面,创建MainActivity.java

      

     1 package com.example.helloworld;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.view.Menu;
     6 import android.view.MenuItem;
     7 import android.widget.Button;
     8 import android.widget.TextView;
     9 import android.support.v4.app.NavUtils;
    10 
    11 
    12 public class MainActivity extends Activity {
    13 
    14     @Override
    15     public void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18         TextView myTextView=(TextView)findViewById(R.id.myTextView);
    19         Button myButton=(Button)findViewById(R.id.myButton);
    20         myTextView.setText("我的第一个Android程序");
    21         myButton.setText("确定");
    22     }
    23 
    24     @Override
    25     public boolean onCreateOptionsMenu(Menu menu) {
    26         getMenuInflater().inflate(R.menu.activity_main, menu);
    27         return true;
    28     }
    29 
    30    
    31 }

    2、在res目录下面创建MainActivity.java的布局文件,activity_main.xml

      

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent" >
     5 
     6     <TextView
     7         android:id="@+id/myTextView"
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:layout_centerHorizontal="true"
    11         android:layout_centerVertical="true"
    12         android:padding="@dimen/padding_medium"
    13         tools:context=".MainActivity" />
    14 
    15         <Button
    16             android:id="@+id/myButton"
    17            android:layout_width="fill_parent"
    18          android:layout_height="wrap_content"/>
    19 </RelativeLayout>

    3、gen下面的R.java是工具自动帮我们生成的一个全局性文件,如果工程里面有任何一个错误,都会导致R.java生成失败。

      

     1 package com.example.helloworld;
     2 
     3 public final class R {
     4     public static final class attr {
     5     }
     6     public static final class dimen {
     7         public static final int padding_large=0x7f040002;
     8         public static final int padding_medium=0x7f040001;
     9         public static final int padding_small=0x7f040000;
    10     }
    11     public static final class drawable {
    12         public static final int ic_action_search=0x7f020000;
    13         public static final int ic_launcher=0x7f020001;
    14     }
    15     public static final class id {
    16         public static final int menu_settings=0x7f080002;
    17         public static final int myButton=0x7f080001;
    18         public static final int myTextView=0x7f080000;
    19     }
    20     public static final class layout {
    21         public static final int activity_main=0x7f030000;
    22     }
    23     public static final class menu {
    24         public static final int activity_main=0x7f070000;
    25     }
    26     public static final class string {
    27         public static final int app_name=0x7f050000;
    28         public static final int hello_world=0x7f050001;
    29         public static final int menu_settings=0x7f050002;
    30         public static final int title_activity_main=0x7f050003;
    31     }
    32     public static final class style {
    33         public static final int AppTheme=0x7f060000;
    34     }
    35 }

    4、工程主目录下面的AndroidManifest.xml,注册这样的一个Activity

     1 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     2     package="com.example.helloworld"
     3     android:versionCode="1"
     4     android:versionName="1.0" >
     5 
     6     <uses-sdk
     7         android:minSdkVersion="4"
     8         android:targetSdkVersion="15" />
     9 
    10     <application
    11         android:icon="@drawable/ic_launcher"
    12         android:label="@string/app_name"
    13         android:theme="@style/AppTheme" >
    14         <activity
    15             android:name=".MainActivity"
    16             android:label="@string/title_activity_main" >
    17             <intent-filter>
    18                 <action android:name="android.intent.action.MAIN" />
    19 
    20                 <category android:name="android.intent.category.LAUNCHER" />
    21             </intent-filter>
    22         </activity>
    23     </application>
    24 
    25 </manifest>
    --------------------------------------------------------------------------------------------------------------------------------------------
    顺势而为
  • 相关阅读:
    jQuery
    BOM与DOM操作
    剑指offer 面试题10.1:青蛙跳台阶
    剑指offer 面试题10:斐波那契数列
    剑指offer 面试题9.1:用两个队列实现栈
    剑指offer 面试题9.1:用两个队列实现栈
    剑指offer 面试题9:用两个栈实现队列
    剑指offer 面试题8:二叉树的下一个节点
    剑指offer 面试题7:重建二叉树
    剑指offer 树的基本操作:四种遍历方式
  • 原文地址:https://www.cnblogs.com/zhuzhenyu/p/2611871.html
Copyright © 2020-2023  润新知