2011-06-25 45 views
0

我的應用程序是如何工作的,一旦用戶打開它,它將打開一個屏幕,根據用戶點擊的按鈕,他們想玩多少洞高爾夫球(18或9)將啓動主要活動,取決於用戶選擇的內容將取決於應用程序的規則。即 - 如果他們選擇18,那麼保存按鈕將不會激活,直到第18個洞,相同的9,它會激活第9洞。這也會觸發最終比分通知等。字符串數組還是新類?

我很好奇,如果我應該爲9洞和18洞創建一個單獨的類,或者如果我應該從打開的屏幕傳遞某種值到主要活動將值設置爲9或18?

我想我很好奇這個編程禮儀,因爲我不是很熟悉這樣的最佳實踐。

輸入屏幕會是這個樣子,截至目前(我還沒有完成9洞按鈕或幫助按鈕,但將是相同的18,除非推出一個單獨的類)如果

case R.id.button18Holes: 

     //*********************************// 
     //***LAUNCHES ACTUAL APPLICATION***// 
     //*********************************// 
     Intent myIntent = new Intent(src.getContext(), EasyPar.class); 
     startActivityForResult(myIntent, 0); 

     Intent iStartValues = new Intent(this, EasyPar.class); 
     String[] startValues = new String[] {"18"}; 
     iStartValues.putExtra("strings", startValues); 
     startActivity(iStartValues); 

     break; 

    case R.id.button9Holes: 
     break; 

    case R.id.buttonHelp: 
     break; 
    } 

林不知道字符串數組是將一個傳遞給另一個活動的正確方法?

在此先感謝!

+0

我覺得這不值得寫一個完整的答案,但讓我們說你創建一個單獨的活動9和18洞遊戲。這兩個班級會共享菜單等常見項目嗎?其他活動的鏈接?如果是這樣,我會考慮擴展Application類以存儲應用程序範圍的變量,例如正在播放的孔的數量。然後,只需編寫一個活動即可切換到必要的位置,以便將其定製到用戶的遊戲中。 –

+0

謝謝!我目前正在構建一個擴展應用程序的globalvariables類。 – Rob

回答

0

純粹的OO人會說你應該創建一個包含常用操作和字段的抽象基類,然後爲專業化創建子類。案例陳述和上面的陳述如上所述不是純粹的面向對象。

一般情況下數組是一樣的 - 在純粹的OO中,您可能會將它們作爲類中的字段,但是對它們執行的任何操作都將在類中進行。

就我個人而言,我會說任何你認爲會更容易維護,更快速的編程和更明顯的閱讀代碼的其他人。我想這並不真正回答這個問題,但:-)

+0

謝謝你的建議! – Rob

0

你絕對不應該只使用一個數組只有兩個對象!這是矯枉過正。這很重要,因爲你在移動設備上使用的內存很少,陣列會佔用一些內存。你也應該使用按鈕監聽器而不是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很簡單的複製/粘貼東西

相關問題