【问题标题】:Second button not working, but the first does第二个按钮不起作用,但第一个按钮起作用
【发布时间】:2013-12-15 15:15:23
【问题描述】:

我创建了一个 main.xml,其中有 4 个按钮,然后,我创建了 Activity,我在其中为第一个按钮编写了一个 Intent 以打开一个新的 Activity,并且它起作用了。但是,我想为第二个按钮做同样的事情,但它不像第一个按钮那样做......我的答案是为什么?为什么我要改变做同样的动作?

这里是代码..

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"

    >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/noua" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/zece" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/unspe" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/doispe" />


</LinearLayout>

MainActivity.java:

package com.orar;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

    //implement the OnClickListener interface
    public class MainActivity extends Activity
    implements OnClickListener {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            //get the Button reference
            //Button is a subclass of View 
            //buttonClick if from main.xml "@+id/buttonClick"
            View btnClick = findViewById(R.id.button1);
            //set event listener
            btnClick.setOnClickListener(this);
        }

        //override the OnClickListener interface method
        @Override
        public void onClick(View arg0) {
            if(arg0.getId() == R.id.button1){
                //define a new Intent for the second Activity
                Intent intent = new Intent(this,SecondActivity.class);
                //start the second Activity
                this.startActivity(intent);
            }
        }

从这里开始第二个按钮和问题...

        //implement the OnClickListener interface
        public class MainActivity  extends Activity
        implements OnClickListener {
            /** Called when the activity is first created. */
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);

                //get the Button reference
                //Button is a subclass of View 
                //buttonClick if from main.xml "@+id/buttonClick"
                View btnClick = findViewById(R.id.button2);
                //set event listener
                btnClick.setOnClickListener(this);
            }

            //override the OnClickListener interface method
            @Override
            public void onClick(View arg0) {
                if(arg0.getId() == R.id.button2){
                    //define a new Intent for the second Activity
                    Intent intent2 = new Intent(this,ThirdActivity.class);
                    //start the second Activity
                    this.startActivity(intent2);
                }
            }
        }
    }

SecondActivity.java:

package com.orar;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.second);

    }
}

ThirdActivity.java:

package com.orar;

import android.app.Activity;
import android.os.Bundle;

public class ThirdActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.third);

    }
}

second.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_width="match_parent"
  android:layout_height="match_parent"
  android:text="This is the second Activity"
/>
</LinearLayout>

第三个.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_width="match_parent"
  android:layout_height="match_parent"
  android:text="This is the third Activity"
/>
</LinearLayout>

【问题讨论】:

  • 您的活动中没有第二个按钮的调用。您能否分享一下您尝试过的方法,并准确告诉我们哪些方面没有按预期工作?
  • 我应该如何调用我的活动中的第二个按钮?我试图改变一些东西以使第二个按钮开始一个新的活动,但我没有..我应该在第一个按钮的代码中更改什么以对第二个按钮进行相同的操作?

标签: android button android-intent android-activity


【解决方案1】:

你有很多问题。首先,你只为你的第一个按钮设置一个点击监听器:

//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.button1);
//set event listener
findViewById(R.id.button1).setOnClickListener(this);

你应该为你的四个按钮做这件事:

findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);

然后,第二个问题出在 onClick 方法上,只有在单击按钮一时才会执行操作。如果单击四个按钮之一,您应该执行操作:

@Override
public void onClick(View arg0) {
    // create a general intent
    Intent intent = null;
    // define an intent for all cases
    switch(arg0.getId()){
        case R.id.button1:
            // Setting intent for first button
            intent = new Intent(this,SecondActivity.class);
            break;
        case R.id.button2:
            // Setting intent for second button
            intent = new Intent(this,ThirdActivity.class);
            break;
        case R.id.button3:
            // Setting intent for third button
            intent = new Intent(this,ThirdActivity.class);
            break;
        case R.id.button4:
            // Setting intent for fourth button
            intent = new Intent(this,ThirdActivity.class);
            break;
    }
    // start the Activity selected at the end
    this.startActivity(intent);
}

【讨论】:

  • 但是创建完这个case之后,每个case我要怎么做才能打开另一个不一样的Activity呢?
  • 您可以更改 onClick 方法中每个按钮的案例中的文本。因此,如果您想在单击按钮 3 时启动不同的活动,您可以在 case R.id.button3: Intent intent = ...; break; 下编写一个新意图
  • 注意gahfy。您在每种情况下都重新声明了 Intent 意图。使用你在 onCLick 方法上声明的那个,否则程序会因为 NullPointerException 而崩溃
  • 是的,你是对的 Tommaso Resti 感谢你和 Fllo(他编辑了我的代码)。我复制/粘贴错误,抱歉。
【解决方案2】:

您可以做的是在您的主要活动中创建一个名为onButtonPressed() 的方法。它将处理我们的按钮按下事件。

public void onButtonPressed(View v) {
    Intent startActivityIntent = null;
    switch (v.getId()) {

        case R.id.button1:
            startActivityIntent = new Intent(this, SecondActivity.class);
        break;

        case R.id.button2:
            startActivityIntent = new Intent(this, ThirdActivity.class);
        break;

        case R.id.button3:
        break;

        case R.id.button4:
        break;
    }
    if(startActivityIntent != null)
    startActivity(startActivityIntent);
}

现在在main.xml中添加按钮的onClick属性为

<?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"
    android:onClick="onButtonPressed" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/noua"
    android:onClick="onButtonPressed" />

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/zece" />

<Button
    android:id="@+id/button3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unspe"
    android:onClick="onButtonPressed" />

<Button
    android:id="@+id/button4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/doispe"
    android:onClick="onButtonPressed" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 2021-06-03
    • 1970-01-01
    • 2013-06-19
    • 2023-02-09
    • 2018-12-31
    • 2021-03-29
    • 1970-01-01
    相关资源
    最近更新 更多