多个按钮w /每个选择一个不同的活动

时间:2014-10-30 03:07:59

标签: java android eclipse

我是新手编码,当我的主要活动页面有一系列按钮时,我正在尝试处理项目。从这些按钮我希望每个按钮打开一个不同的活动或命令。我一直在寻找并找到我认为应该工作的东西,但事实并非如此。当它加载应用程序然后单击按钮时,我遇到了崩溃。下面是代码。任何在某处显示我的错误的指针都会受到赞赏。

package com.example.finalproject2;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

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

    //Initialize Buttons
    Button b1 = (Button) findViewById(R.id.button1);
    Button b2 = (Button) findViewById(R.id.button02);
    Button b3 = (Button) findViewById(R.id.button03);
    Button b4 = (Button) findViewById(R.id.button01);
    Button b5 = (Button) findViewById(R.id.button04);

    //Set OnCLickListeners
    b1.setOnClickListener(chicagoListener); 
    b2.setOnClickListener(sanJoseListener); 
    b3.setOnClickListener(baltimoreListener); 
    b4.setOnClickListener(westPalmBeachListener);   
    b5.setOnClickListener(websiteListener); }

private OnClickListener chicagoListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, Chicago.class));
    }
};
private OnClickListener sanJoseListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, SanJose.class));
    }
};

private OnClickListener baltimoreListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, Baltimore.class));
    }
};
private OnClickListener westPalmBeachListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
    }
};
private OnClickListener websiteListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
    }
};




        {
}





}

2 个答案:

答案 0 :(得分:1)

在您的应用宣言文件中提及您的所有活动(如下所示):

<activity
      android:name="com.example.finalproject2.Chicago"
      android:label="@string/app_name" >
</activity>

尝试做这样的事情:

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


    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    b4.setOnClickListener(this);   
    b5.setOnClickListener(this);
}



@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.button1:
        startActivity(new Intent(MainActivity.this, Chicago.class));
        break;

    case R.id.button02:
        startActivity(new Intent(MainActivity.this, SanJose.class));
        break;

    case R.id.button03:
        startActivity(new Intent(MainActivity.this, Baltimore.class));
        break;

    case R.id.button01:
        startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
        break;

    case R.id.button04:
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
        break;  

    }
}

答案 1 :(得分:0)

Button - Android developer

来自开发者网站的样本

    Button button = (Button) findViewById(R.id.button_send);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         // Do something in response to button click
        }
    });

another good example of how to use onclick