0

我正在尝试在主页上创建一个带有 4 个按钮的应用程序。每个按钮都链接到一个新的 xml 页面,但是我在使用“AddListenerOnButton”时遇到了麻烦。它适用于第一个,但由于某种原因不是 2.,3。第四。我已经用 4 个 xml 按钮/页面引用的每个活动更新了 xml 清单。

正如您在我的代码中看到的那样,每个 xml 页面都有自己的 .class。

这是我的主要活动:

package com.example.nutritiontest;

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


public class MainActivity extends Activity {
    ImageButton imagebtn1;
    ImageButton imagebtn2;
    ImageButton imagebtn3;
    ImageButton imagebtn4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
    }

    public void addListenerOnButton() {
        imagebtn1 = (ImageButton) findViewById(R.id.knapA);
        imagebtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent
                        (getApplicationContext(), activityA.class);
                    startActivity(intent); 
            }
        });
    }

    public void addListenerOnButton1() {
        imagebtn2 = (ImageButton) findViewById(R.id.knapB);
        imagebtn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent
                        (getApplicationContext(), activityB.class);
                    startActivity(intent); 
            }
        });
    }

public void addListenerOnButton2() {
    imagebtn3 = (ImageButton) findViewById(R.id.knapC);
    imagebtn3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent
                    (getApplicationContext(), activityC.class);
                startActivity(intent); 
        }
    });
}

public void addListenerOnButton3() {
    imagebtn4 = (ImageButton) findViewById(R.id.knapD);
    imagebtn4.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent
                    (getApplicationContext(), activityD.class);
                startActivity(intent); 
        }
    });
}

}

我对 android 开发非常陌生,因此将不胜感激所有帮助

4

5 回答 5

2

您只调用 addListenerOnButton() 方法。您还需要添加对其他 3 个方法的调用(addListenerOnButton1、addListenerOnButton2addListenerOnButton3)。

于 2013-10-28T14:33:18.527 回答
2

你只调用初始化第一个按钮的方法,你也必须调用其他三个!

尝试这个:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();
        addListenerOnButton1();
        addListenerOnButton2();
        addListenerOnButton3();
    }
于 2013-10-28T14:34:20.133 回答
1

您应该将这 3 个调用添加到您的 onCreate 方法

addListenerOnButton1()
addListenerOnButton2()
addListenerOnButton3()
于 2013-10-28T14:33:26.703 回答
0

我建议使用与监听器相同的活动:

public class MainActivity extends Activity implements OnClickListener{
...
public void onCreate(Bundle savedInstanceState) {
    ...
    imagebtn1.setOnClickListener(this)
    imagebtn1.setOnClickListener(this)
    imagebtn1.setOnClickListener(this)
    imagebtn1.setOnClickListener(this)
    ...
}

然后在 OnClick 上,只需使用简单的 if/elseif 按钮过滤掉:

@Override
    public void onClick(View arg0) {
    if(view == imagebtn1){
        //do stuff
    }else if (view == imagebtn2){
        //do stuf
    }else if (view == imagebtn3){
        //do stuf
    }else if (view == imagebtn4){
        //do stuf
    }
}
于 2013-10-28T14:40:54.743 回答
0

其他答案已经解决了您当前的问题,但我想提供另一种方法来做到这一点,恕我直言,这将是更清洁和更少的代码。

首先,只需将相同的内容添加到您listener的所有ButtonsonCreate()

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imagebtn1.setOnClickListener(this);
    imagebtn2.setOnClickListener(this);
    ...
}

然后创建onClick()方法并打开单击并更改您想要去的idButton

 @Override
 public void onClick(View v)
 {
     Intent intent = new Intent();
     String nextAct = null;                    // name for Activity to start with Intent
     String shield = "com.example.nutritiontest.";  // set package name
     int flag = -1;

     switch (v.getId())  // get the id of the View clicked
                             // and compare below
     {
        case (R.id.knapA):
           nextAct = package + "activityA";
           break;
        case (R.id.knapB):
           nextAct = package + "activityB";
           break;
        case (R.id.knapC):
           nextAct = package  + "activityC"; 
           break;
           default:
           Toast.makeText(MainActivity .this, "Item currently not available", Toast.LENGTH_SHORT).show();
    }
    try 
    {
    if (nextAct != null)
    {
        intent = new Intent(MainActivity .this, Class.forName(nextAct));
        flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
        if (flag != -1)
        {   intent.setFlags(flag);  }
        startActivity(intent);
    }
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

那么您将要实施OnClickListener

public class MainActivity extends Activity implements OnClickListener {

一开始它可能看起来很复杂或凌乱,但一旦你分解它就真的不是了。Flags如果不需要,您甚至可以使用 删除这些位。我打算用它们做更多,所以我现在添加它们。您甚至可以删除引用包的部分,并Intent在每个部分中添加您需要的部分,case如果它更容易的话。

于 2013-10-28T14:48:57.570 回答