2011-08-08 131 views
0

我在哪裏可以在我的主要活動課程中聲明自定義按鈕?它可以在onStart方法上,還是隻能在onCreate方法中使用?Android活動問題

任何幫助將不勝感激?

還要進一步說明我在說什麼:這是我的活動課。

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      if(D) Log.e(TAG, "+++ ON CREATE +++"); 
      setContentView(R.layout.main); 



      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
      if (mBluetoothAdapter == null) { 
       Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show(); 
       finish(); 
       return; 
       }   
     } 
    @Override 
     public void onStart() { 
      super.onStart(); 
      if(D) Log.e(TAG, "++ ON START ++"); 
      // If BT is not on, request that it be enabled. 
      if (!mBluetoothAdapter.isEnabled()) { 
       Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
       startActivityForResult(enableIntent, REQUEST_ENABLE_BT); 
       } 
      else { 
       startApp(); 
       } 
      } 
private void startApp(){ 

     View Patient_Button = findViewById(R.id.patientButton); 
     Patient_Button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
      //case R.id.patientButton: 
       //Intent b = new Intent(getApplicationContext(), Detailed_ModeActivity.class); 
       //startActivity(b); 
       //break; 
       } 
      } 
     ); 
     View Doctor_Button = findViewById(R.id.doctorButton); 
     Doctor_Button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent a = new Intent(getApplicationContext(), Detailed_ModeActivity.class); 
       startActivity(a); 
       //break; 
       } 
      } 
     ); 
     View About_Option = findViewById(R.id.aboutButton); 
     About_Option.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent c = new Intent(getApplicationContext(), About.class); 
       startActivity(c); 
       //break; 
       } 
      } 
     ); 

*

main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@color/background" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="85dip" 
    android:orientation="horizontal"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" android:orientation="vertical"> 
     <TextView 
      android:text="@string/mainTitle" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_marginBottom="25dip" 
      android:textSize="24.5sp"/> 


     <!-- Patient Option --> 
     <Button android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
      android:text="@string/patientButton" 
      android:id="@+id/patientButton"> 
     </Button> 

     <!-- Doctor Option --> 
     <Button android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/doctorButton" 
      android:layout_weight="1" 
      android:id="@+id/doctorButton"> 
     </Button> 

     <!-- Exit Mode --> 
     <Button android:text="@string/exit" 

     android:layout_weight="1" 
     android:id="@+id/exit" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"></Button> 

     <!-- About Mode --> 
     <Button android:text="@string/aboutButton" 

     android:layout_weight="1" 
     android:id="@+id/aboutButton" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"></Button> 

    </LinearLayout> 



</LinearLayout> 
+0

這取決於你如何聲明你的自定義按鈕。在代碼或XML資源中。 –

+0

我在 – YoKaGe

+0

中聲明它通常你會在onCreate()中聲明它。也許你應該考慮爲按鈕製作一個控制器。使用onCreate()中的所需視圖實例化控制器。 –

回答

2

在Android中,相信它是常規的聲明在onCreate()方法的按鈕。正如@Mike D所說的,你也可以創建一個控制器並在onCreate()方法中實例化這個控制器。雖然你的問題並不太清楚你在使用它的時候遇到了什麼問題,但是你似乎只是在這些選擇之間尋找答案onStart()和onCreate()。

例如,

public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button sampleButton = (Button) findViewById(R.id.sampleButton); 
} 

在這種情況下,你會在你的main.xml文件中聲明的按鈕,這個按鈕的id是sampleButton

此外,爲了將來的參考,您需要查看此頁面上的chart以查看Android Activity生命週期以及放置位置。這不僅可以回答這個問題,而且可以給你自己研究這樣一個概念的空間。

0

RoboGuice試圖加快你的發展,所以你不必擔心這些小細節太多。

+0

請詳細說明鏈接中的內容 – Freakyuser

+0

當然。 RoboGuice通過處理大部分初始化請求來加速開發。因此,在onCreate(...)方法中,您不需要爲所有資源(即按鈕等)查找ViewById(...)。 Wiki有一個很好的示例和進一步的解釋https:// github .com/roboguice/roboguice/wiki – drunkenRabbit

+0

ahaa,我希望你編輯你自己的答案,並將上述評論的上述內容附加到答案上。然後等待提問者接受你的回答或其他人的回覆。 – Freakyuser