• 第一个Android程序—认识文件结构


    该程序任务:添加一个显示文本和一个按钮

    对Activity的初步认识:就像一个窗口,能显示信息,又像一个容器,能容纳功能空间,如button,在程序角度上看,又像一个 类,可以和其他的类(Activity)发生联系。

    创建Activity的要点:

    1. 一个Activity就是一个类,类名随意起,不过必须继承Activity这个父类。
    2. 需要复写onCreate()方法
    3. 每一个Activity都应该在AndroidManifest.xml文件中进行配置
    4. 为Activity添加必要的控件

    整体文件代码预览:

     1 //MyActivity.java 文件
     2 
     3 package geeker.MyActivity;
     4 import android.app.Activity;
     5 import android.os.Bundle;
     6 import android.widget.Button;
     7 import android.widget.TextView;
     8 
     9 public class MyActivity extends Activity {
    10     //成员变量的声明
    11     private TextView myTextView = null;
    12     private Button myButton = null;
    13     //重写OnCreate方法,会自动生成
    14     public void onCreate(Bundle savedInstanceState) {
    15         //调用父类方法,该句代码自动生成
    16         super.onCreate(savedInstanceState);
    17         //通过布局文件的id调用该Activity所使用的布局文件
    18         setContentView(R.layout.main);
    19         //通过findViewById()方法拿到布局文件中添加的控件
    20         //不过在布局文件中添加控件的时候必须定义id号,
    21         //如:android:id="@+id/myTextView"
    22         myTextView = (TextView)findViewById(R.id.myTextView);
    23         myButton = (Button)findViewById(R.id.myButton);
    24         //向控件上制定显示文字
    25         myTextView.setText("This is my first Activity !");
    26         myButton.setText("MY FIRST BUTTON");  
    27     }
    28 }
    29 
    30 //main.xml 文件
    31 <?xml version="1.0" encoding="utf-8"?>
    32 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    33     android:orientation="vertical"
    34     android:layout_width="fill_parent"
    35     android:layout_height="fill_parent"
    36     >
    37 <TextView  
    38     android:id="@+id/myTextView"
    39     android:layout_width="fill_parent" 
    40     android:layout_height="wrap_content" 
    41     />
    42 <Button
    43     android:id="@+id/myButton"
    44     android:layout_width="fill_parent"
    45     android:layout_height="wrap_content"
    46     />    
    47 </LinearLayout>
    48 
    49 //R。jar 文件该文件自动生成,不要自己改动
    50 /* AUTO-GENERATED FILE.  DO NOT MODIFY.
    51  *
    52  * This class was automatically generated by the
    53  * aapt tool from the resource data it found.  It
    54  * should not be modified by hand.
    55  */
    56 
    57 package geeker.MyActivity;
    58 
    59 public final class R {
    60     public static final class attr {
    61     }
    62     public static final class drawable {
    63         public static final int icon=0x7f020000;
    64     }
    65     public static final class id {
    66         public static final int myButton=0x7f050001;
    67         public static final int myTextView=0x7f050000;
    68     }
    69     public static final class layout {
    70         public static final int main=0x7f030000;
    71     }
    72     public static final class string {
    73         public static final int app_name=0x7f040001;
    74         public static final int hello=0x7f040000;
    75     }
    76 }

    其实走一遍添加Button的流程就明白各个文件间的联系了:

    1 先打开main.xml文件,加一个button按钮的布局

    1 <Button
    2     android:id="@+id/myButton"
    3     android:layout_width="fill_parent"
    4     android:layout_height="wrap_content"
    5  />  

    2 其实上一步完成后,编译运行已经能看到一个Button按钮了,但是我想在按钮上添加文字以说明该按钮的作用,在java中的程序为 :

    Button bt = new Button();
    bt.setText("MY FIRST BUTTON");

    那么在Android程序中如何在.java源文件中拿到刚才在main.xml中添加的控件呢?

    基于这个目的,在main.xml文件中加了此句:android:id="@+id/myButton",这一句使得R.java文件中多了一个叫id的类,该控件的id号就在这个类中出现了,这样做为了方便.java文件中的调用。

    实际上如果不加上一句,该控件是不会在R.java文件中产生id号码的,因为只有在res目录中添加文件才会自动在R.java中产生id号,而添加一个控件只是在一个资源文件中做修改而已,所以不会自动产生id号。

    我们可以看一下R.java文件中自动产生的ID代码:

     public static final class id 
    {
            public static final int myButton=0x7f050001;
            public static final int myTextView=0x7f050000;
    }

    然后在.java文件中就可通过getViewById()方法拿到控件了

    拿到控件之后就可以像java程序中一样进行相关操作了,代码如:

    private Button myButton = null;
    
    myButton = (Button)findViewById(R.id.myButton);
    
    myButton.setText("MY FIRST BUTTON");  

    实际上这个流程只体现了xml文件和R.java文件之间的联系(通过该句:android:id="@+id/myButton),以及.java与R.java之间的联系(通过该句:findViewById(R.id.myTextView) )

    补充一下其他文件关系的代码体现:

    MyActivity.java文件与Main.xml文件的联系时通过MyActivity.java文件中的setContentView(R.layout.main);体现的,因为一个Activity文件要对应一个布局文件

    MyActivity.java文件与AndroidManifest.xml文件之间的联系时通过AndroidManifest.xml文件中的

    <activity android:name=".MyActivity" 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创建的关键点之一:

    • 每一个Activity都应该在AndroidManifest.xml文件中进行配置
  • 相关阅读:
    物联网操作系统HelloX开发者入门指南
    【 D3.js 高级系列 】 总结
    【 D3.js 高级系列 — 10.0 】 思维导图
    android fragment+ FragmentTabHost+viewpager 切换状态不保存的问题
    OpenGL 顶点缓存对象
    OpenGL顶点数组
    【 D3.js 高级系列 — 9.0 】 交互式提示框
    如何在 Linux 上录制你的终端操作
    程序员诗词大赛开始了_你看过吗?
    程序员与代码的搞笑日常
  • 原文地址:https://www.cnblogs.com/smart9595/p/3721122.html
Copyright © 2020-2023  润新知