Android HelloWord編寫方式介紹
相信學(xué)過編程的人員都對各種語言的helloword程序的編寫方式記憶猶新吧。在這里我們就為大家詳細(xì)介紹一下有關(guān)Android HelloWord的編寫方式,方便大家對這一操作系統(tǒng)編寫方式的理解。#t#
先說說整個(gè)程序要做哪些內(nèi)容吧,簡單helloword 通過一個(gè)按鈕點(diǎn)擊在另一個(gè)acitvity出現(xiàn)文本Hello xiaoshengDAI
說下Android HelloWord做的步驟吧:
1.首先新建項(xiàng)目,我這邊主要是測試Layout所以項(xiàng)目名就叫這個(gè)了。
2.我們要顯示一個(gè)按鈕,難后點(diǎn)擊這個(gè)按鈕就轉(zhuǎn)到其他activity顯示Hello xiaoshengDAI,新建類Layout主要來顯示***個(gè)activity即button,
1).在main.xml文件中進(jìn)行配置
Java代碼
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- < Button android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="來點(diǎn)我吧"/>
- < /LinearLayout>
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- < Button android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="來點(diǎn)我吧"/>
- < /LinearLayout>
2).設(shè)置監(jiān)聽和跳轉(zhuǎn)actiovity
Java代碼
- package com.layout;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Layout extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- OnClickListener listener1 = null;
- Button botton1 = null;
- listener1 = new OnClickListener(){
- public void onClick(View v) {
- Intent intent0 = new Intent(Layout.this,
ActivityFrameLayout.class);- setTitle("FrameLayout");
- startActivity(intent0);
- }
- };
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- botton1 = (Button) findViewById(R.id.button1);
- botton1.setOnClickListener(listener1);
- }
- }
- package com.layout;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class Layout extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- OnClickListener listener1 = null;
- Button botton1 = null;
- listener1 = new OnClickListener(){
- public void onClick(View v) {
- Intent intent0 = new Intent(Layout.this,
ActivityFrameLayout.class);- setTitle("FrameLayout");
- startActivity(intent0);
- }
- };
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- botton1 = (Button) findViewById(R.id.button1);
- botton1.setOnClickListener(listener1);
- }
- }
3.在Android HelloWord編寫中,新建activityFrameLayout類和activityFrameLayout.xml文件
Java代碼
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- < TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Hello xiaoshengDAI"
- />
- < /LinearLayout>
- < ?xml version="1.0" encoding="utf-8"?>
- < LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- < TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Hello xiaoshengDAI"
- />
- < /LinearLayout>
Java代碼
- package com.layout;
- import android.app.Activity;
- import android.os.Bundle;
- public class ActivityFrameLayout extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("哈哈");
- setContentView(R.layout.activityframelayout);
- }
- }
- package com.layout;
- import android.app.Activity;
- import android.os.Bundle;
- public class ActivityFrameLayout extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setTitle("哈哈");
- setContentView(R.layout.activityframelayout);
- }
- }
4.對AndroidManifest.xml進(jìn)行配置,將新建Activity配置文件加進(jìn)來
Android HelloWord的Java代碼
- < ?xml version="1.0" encoding="utf-8"?>
- < manifest xmlns:android=
"http://schemas.android.com/apk/res/android"- package="com.layout"
- android:versionCode="1"
- android:versionName="1.0">
- < application android:icon="@drawable/icon"
android:label="@string/app_name">- < activity android:name=".Layout"
- 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 android:name=".ActivityFrameLayout"
android:label="activityFrameLayout">- < intent-filter>
- < action android:name="android.intent.action.MAIN" />
- < category android:name=
"android.intent.category.LAUNCHER" />- < /intent-filter>
- < /activity>
- < /application>
- < uses-sdk android:minSdkVersion="3" />
- < /manifest>
- < ?xml version="1.0" encoding="utf-8"?>
- < manifest xmlns:android=
"http://schemas.android.com/apk/res/android"- package="com.layout"
- android:versionCode="1"
- android:versionName="1.0">
- < application android:icon="@drawable/icon"
android:label="@string/app_name">- < activity android:name=".Layout"
- 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 android:name=".ActivityFrameLayout"
android:label="activityFrameLayout">- < intent-filter>
- < action android:name="android.intent.action.MAIN" />
- < category android:name=
"android.intent.category.LAUNCHER" />- < /intent-filter>
- < /activity>
- < /application>
- < uses-sdk android:minSdkVersion="3" />
- < /manifest>
5.Android HelloWord可以運(yùn)行了,嘿嘿