《android 4高级编程》--第2章 开始入手 "又是一个hello world"

来源:互联网 发布:kafka java 编辑:程序博客网 时间:2024/05/21 10:52

熟悉用eclipse开发应用,在我之前的写的 eclipse 搭建android环境补充有说明。

其实开始比较简单无非就是了解eclipse怎么安装利用ADT插件迅速搭建一个工程,然后运行调试显示结果。


下面是我搭建环境写的第一个应用"hello world"-------------->什么东西开始都是一个hello world,以后有机会开个咖啡店,名字就是“hello world".得意

代码如下:


package com.paad.helloworld;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView myTextViem = (TextView)findViewById(R.id.myTextViem);
myTextViem.setText(getString(R.string.hello_world));


}


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


}


activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


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


</RelativeLayout>


strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">PA4AD_CH02_Hello_world</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>


</resources>

最主要的布局文件:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.paad.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >


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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.paad.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


原创粉丝点击