如何使两个按钮打开2个布局

时间:2014-12-23 17:09:52

标签: 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); }

}

2 个答案:

答案 0 :(得分:1)

Button  bntoartistpage = (Button) findViewById(R.id.bntoartistpage);
bntoartistpage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        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);
    }
});

Button bntonewmp3page = (Button) findViewById(R.id.bntonewmp3page);
bntonewmp3page.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        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);
    }
});

答案 1 :(得分:1)

您无法在一个活动中使用两种onClick()方法。使用此

@Override
public void onClick(View view) {
    if(view.getId() == R.id.bntoartistpage)
    {
        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); 
    }
    else if(view.getId() == R.id.bntonewmp3page)
    {
        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);
    }
}