如何让两个按钮打开两个活动

时间:2014-12-24 05:40:10

标签: android

package com.example.luke.sinhalasindu;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


    public class HomePage extends Activity implements OnClickListener {

        Button bntoartistpage;
        Button bntonewmp3page;


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

            bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
            bntoartistpage.setOnClickListener(this);

            bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
            bntonewmp3page.setOnClickListener(this);
        }


        @Override
        public void onClick(View view) {
            Intent inent = new Intent(this, Artist.class);

            // calling an activity using <intent-filter> action name
            //  Intent inent = new Intent("com.example.luke.sinhalasindu");
            startActivity(inent); }

        @Override

        public void onClick(View view){
            Intent inent = new Intent(this, NewMp3.class);
            // calling an activity using <intent-filter> action name
            //  Intent inent = new Intent("com.example.luke.sinhalasindu");
            startActivity(inent); 
        }
    }
}

5 个答案:

答案 0 :(得分:1)

而不是这段代码:

 bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
        bntoartistpage.setOnClickListener(this);

        bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
        bntonewmp3page.setOnClickListener(this);

试试此代码

bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
         bntoartistpage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent inent = new Intent(HomePage.this, Artist.class);

                // calling an activity using <intent-filter> action name
                //  Intent inent = new Intent("com.example.luke.sinhalasindu");
                startActivity(inent);
            }
        });

bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
bntonewmp3page.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Intent inent = new Intent(HomePage.this, NewMp3.class);
                // calling an activity using <intent-filter> action name
                //  Intent inent = new Intent("com.example.luke.sinhalasindu");
                startActivity(inent); 
            }
        });

并在Activity文件中声明这两个ArtistNewMp3AndroidManifest.xml,如下所示:

<activity android:name=" com.example.luke.sinhalasindu.Artist" />
<activity android:name=" com.example.luke.sinhalasindu.NewMp3" />

答案 1 :(得分:1)

无法覆盖onClick方法两次。使用setOnClickListener的内部类或使用此方法:

@Override
public void onClick(View view) {
    Intent inent = null;
    switch(v.getId()) {
        case R.id.bntoartistpage:
            intent = new Intent(ThisAct.this, Activity1.class);      
        break;
        case R.id.bntonewmp3page:
            intent = new Intent(ThisAct.this, Activity2.class);            
        break;
    }
    startActivity(inent); 
}

并在清单中正确注册新活动。

希望这有帮助。

答案 2 :(得分:0)

另一种方法:在onclick()中使用switch-case来选择动作。 v.getId()将返回单击按钮的id。

   @Override
   public void onClick(View v) {
       switch(v.getId()) {
           case R.id.bntoartistpage:
           // do stuff;
           Intent inent = new Intent(HomePage.this, Artist.class);
           startActivity(inent);
           break;
           case R.id.bntonewmp3page:
           // do stuff;
           Intent inent = new Intent(HomePage.this, NewMp3.class);
           startActivity(inent);
           break;

   }

答案 3 :(得分:0)

我从你的代码中了解到你对此感到困惑

  

如何处理覆盖onClick()方法的多个Button侦听器?

您需要做的就是,您必须在if-else方法中应用switch-caseonClick()等任何决策逻辑,如下所示

@Override
public void onClick(View view) {

    switch (view.getId()) {

    case R.id.bntoartistpage:
        Intent inent = new Intent(this, Artist.class);
        startActivity(inent);
        break;

    case R.id.bntonewmp3page:
        Intent inent = new Intent(this, NewMp3.class);
        startActivity(inent); 
        break;
    }

}

答案 4 :(得分:0)

有一种最简单的方法可以让两个或多个不同的按钮进行同样的活动。

当您向OnClickListenerActivity实施Fragment时,您只需要编写onClick()方法一次.. 不像一个按钮的方法。< / strong>您可以处理该方法中的所有OnClickListener ..

因此,在您的情况下,您可以编写如下代码。

public class HomePage extends Activity implements OnClickListener {

Button bntoartistpage;
Button bntonewmp3page;


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

        bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
        bntoartistpage.setOnClickListener(this);

        bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
        bntonewmp3page.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
        case R.id.bntoartistpage:
            startActivity(new Intent(getBaseContext(), NEWCLASS.class)); 
        break;
        case R.id.bntonewmp3page:
            startActivity(new Intent(getBaseContext(), NEWCLASS.class)); 
        break;
        case R.id.OTHERCASES:
            //other functions
        break;

        default:
            break;
        }   
    }
}