你絕對不應該只使用一個數組只有兩個對象!這是矯枉過正。這很重要,因爲你在移動設備上使用的內存很少,陣列會佔用一些內存。你也應該使用按鈕監聽器而不是switch/case語句來查找正在發生的事情。
首先,我強烈建議在潛入Android之前深入研究OOP並學習使用Java的程序的基礎知識。你不必走這條路線,但我會說,如果你選擇不學習基礎知識和基礎知識......爲漫長的艱難道路做準備。
就是這樣說,在Android IMHO中這樣做的最簡單的方法就是這樣......這些評論應該爲您提供足夠的洞察力,以瞭解發生了什麼。
類文件:
GolfTestActivity.class
package com.jmarstudios.golf;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GolfTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This is the main xml layout: res/layout/main.xml
setContentView(R.layout.main);
}
@Override
protected void onStart() {
super.onStart();
// Get a handle to the two buttons in main.xml
final Button _nineHoles = (Button)this.findViewById(R.id.button1);
final Button _eighteenHoles = (Button)this.findViewById(R.id.button2);
// Create a listener for button1
_nineHoles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start the nine hole activity
GolfTestActivity.this.startActivity(new Intent().setClassName("com.jmarstudios.golf", "com.jmarstudios.golf.NineHoleActivity"));
}
});
// Create a listener for button2
_eighteenHoles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start the eighteen hole activity
GolfTestActivity.this.startActivity(new Intent().setClassName("com.jmarstudios.golf", "com.jmarstudios.golf.EighteenHoleActivity"));
}
});
}
}
NineHoleActivity.class
/**
*
*/
package com.jmarstudios.golf;
import android.app.Activity;
import android.os.Bundle;
/**
* @author DDoSAttack
*
*/
public class NineHoleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We simply inflate the layout: res/layout/nineholeslayout.xml
setContentView(R.layout.nineholeslayout);
}
}
EighteenHoleActivity.class
/**
*
*/
package com.jmarstudios.golf;
import android.app.Activity;
import android.os.Bundle;
/**
* @author DDoSAttack
*
*/
public class EighteenHoleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We simply inflate the layout: res/layout/eighteenholeslayout.xml
setContentView(R.layout.eighteenholeslayout);
}
}
,並在XML文件中...
RES /佈局/ main.xml中
<?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="Do you want 9 holes or 18 holes?" />
<Button
android:text="Nine Holes"
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Eighteen Holes"
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
RES /佈局/ nineholeslayout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Nine Holes"
/>
</LinearLayout>
RES /佈局/ eighteenholeslayout.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Eighteen Holes"
/>
</LinearLayout>
最後,您需要將活動添加到您的AndroidManifest.xml文件中
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jmarstudios.golf"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".GolfTestActivity" 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=".NineHoleActivity"></activity>
<activity android:name=".EighteenHoleActivity"></activity>
</application>
</manifest>
這裏是一些便於參考,我強烈建議:
http://developer.android.com/reference/packages.html
http://developer.android.com/reference/android/app/Activity.html
http://developer.android.com/resources/faq/commontasks.html
希望所有幫助,因爲這是p retty很簡單的複製/粘貼東西
我覺得這不值得寫一個完整的答案,但讓我們說你創建一個單獨的活動9和18洞遊戲。這兩個班級會共享菜單等常見項目嗎?其他活動的鏈接?如果是這樣,我會考慮擴展Application類以存儲應用程序範圍的變量,例如正在播放的孔的數量。然後,只需編寫一個活動即可切換到必要的位置,以便將其定製到用戶的遊戲中。 –
謝謝!我目前正在構建一個擴展應用程序的globalvariables類。 – Rob