簡體   English   中英

Android活動問題

[英]Android Activities Question

我在哪里聲明主活動類中的自定義按鈕? 它可以在onStart方法上還是只能在onCreate方法中使用?

任何幫助,將不勝感激?

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

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>

在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

此外,為了將來參考,請查看此頁面上的圖表以查看Android活動生命周期以及放置在何處的內容非常重要。 這不僅可以回答這個問題,還可以讓您自己研究這樣一個概念。

嘗試RoboGuice以加快您的開發速度,因此您不必過多擔心這些細微的細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM