第二个按钮不工作,但第一个按钮不起作用

时间:2013-12-15 15:15:23

标签: android button android-intent android-activity

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

这是代码..

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>

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

2 个答案:

答案 0 :(得分:2)

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

//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方法,只有在单击按钮1时才执行操作。如果单击四个按钮之一,则应执行操作:

@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);
}

答案 1 :(得分:0)

您可以做的就是在主要活动中创建一个名为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" />