Android之旅--HelloWorld进阶,代码分析

        前面已经新建了一个Android项目,并对其结构有了一个简单了解,现在亲自动手操刀,剖析这个简单项目的内部结构。Android开发一般会有下面几步:

  • 建立Android项目
  • 在xml布局文件中设计界面
  • 在java类中实现业务逻辑
  • 在AndroidManifest.xml中添加组件
        下面就按照上面的步骤,一步步分析HelloWorld小项目,并添加一些简单代码,了解Android开发的基础知识。
        在xml布局文件中设计界面

        修改项目中布局文件res/layout/main.xml,修改布局,并添加一个按钮,当点击按钮时,文字改为“Hello World 进阶”,修改后文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/but_ok" />
</LinearLayout>
        根节点LinearLayout是线性布局管理器,里面的组件会安装水平或垂直方向排列,此处android:orientation="vertical"设置为垂直排列,内部两个组件TextView是文本框用于显示文本,Button是新加的按钮。Android界面设计中的组件很多,属性也很多,下面介绍通用的几个属性(其他的偶也不知道):

  • android:id:设置该组件的唯一标识,在java代码中可以通过findViewById(id)获得该组件,格式:@id+/id名称
  • android:layout_width,android:layout_height:指定该组件的宽度高度,可能的取值有:match_parent、fill_parent、wrap_content。如果值为match_parent表明该组件宽度高度与父容器相同,fill_parent与match_parent相同,建议使用match_parent,如果值为wrap_content说明该组件的宽度高度取决于其内容,能包裹它的内容即可。
        android:text="@string/hello_world"表示文本框显示的内容,通过@string/hello_world引用了资源文件中定义的字符串资源,下面是定义的字符串资源文件res/values/strings.xml的内容:

<resources>
    <string name="app_name">HelloWorldTest</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_hello_world_main">HelloWorldMain</string>
    <string name="but_ok">进阶</string>
    <string name="advanced">Hello world 进阶</string>
</resources>

        在java类中实现业务逻辑

先给出java代码:

public class HelloWorldMain extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 设置使用main.xml定义的界面布局
        setContentView(R.layout.main);
        
        // 获得组件
        final TextView text = (TextView) findViewById(R.id.text);
        Button ok = (Button) findViewById(R.id.ok);
        
        // 绑定点击事件监听器
        ok.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// 通过getResources().getString(R.string.advanced),可以直接获得字符串内容
				// text.setText(getResources().getString(R.string.advanced));
				text.setText(R.string.advanced);
			}
		});
    }
}
        代码中注释应该比较清楚,不再细说,下面主要看一下代码中用于访问资源的神秘的R类,代码如下:

public final class R {

    public static final class id {
        public static final int menu_settings=0x7f070002;
        public static final int ok=0x7f070001;
        public static final int text=0x7f070000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int advanced=0x7f040005;
        public static final int app_name=0x7f040000;
        public static final int but_ok=0x7f040004;
        public static final int hello_world=0x7f040001;
        public static final int menu_settings=0x7f040002;
        public static final int title_activity_hello_world_main=0x7f040003;
    }
}
        R中使用不同内部类分别表示不同的类型的资源,如上面用到的id,string,layout,不同资源对应于一个不同的public static final int型常量,在java代码中通过该常量就可以访问到对应的资源。随着项目进行,添加的资源越来越多,R的内容也会越来越多。
         在AndroidManifest.xml中添加组件

        AndroidManifest.xml是整个Android项目的全局描述文件,一般包含如下内容:

  • 应用程序的包名,该包名是应用程序的唯一标识
  • 应用程序包含的组件,如Activity、Service、BroadcastReceiver、ContentProvider等
  • 应用兼容的最低版本
  • 应用程序使用系统所需的权限声明
  • 其他程序访问该程序所需的权限声明

        下面是该小项目的AndroidManifest.xml文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HelloWorldMain"
            android:label="@string/title_activity_hello_world_main" >
            <intent-filter>
                <!-- 设置该activity为程序入口 -->
                <action android:name="android.intent.action.MAIN" />
				<!-- 加载应用时运行该activity -->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值