Android 活动问题

发布于 2024-11-28 10:11:01 字数 4322 浏览 1 评论 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;
                }
            }
        );

*

主.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>

Where do i declare a custom button in my main activity class? Can it be on the onStart Method or will it only work in the onCreate Method?

Any Help would be appreciated?

Also to further illustrate what i am talking about: Here is my activity class.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-12-05 10:11:01

在 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 Activity的生命周期以及什么放在哪里。这不仅可以回答这个问题,还可以给你自己研究这个概念的空间。

In Android, I believe it is conventional to declare a button in the onCreate() method. As @Mike D said, you can also create a controller and instantiate that in this onCreate() method. Although your question is not too clear on what problem you are having with this, it seems that you are simply looking for an answer between those choices- onStart() and onCreate().

For Example,

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

In this scenario, you would have the button declared in your main.xml file and the id of this button would be sampleButton.

Furthermore, for future reference, it is very important that you look at the chart on this page to see the Android Activity life cycle and what to place where. This will not only answer this question, but give you room to study such a concept on your own.

请恋爱 2024-12-05 10:11:01

尝试 RoboGuice 来加快您的开发速度,这样您就不必担心这些小细节了很多。

Give RoboGuice a try to speed up your development and so you don't need to worry about these minor details too much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文