0

メイン アクティビティ クラスのどこでカスタム ボタンを宣言すればよいですか? 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>
4

2 に答える 2

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

さらに、今後の参考のために、このページのチャートを見て、Android アクティビティのライフサイクルと、何をどこに配置するかを確認することが非常に重要です。これはこの質問に答えるだけでなく、そのような概念を自分で研究する余地を与えてくれます。

于 2011-08-09T06:54:30.530 に答える