I have an Activity window in the MainActivity file.
The transition button is processed by the following code:

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); swith.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent SecAct = new Intent(getApplicationContext(), SecondActivity.class); startActivity(SecAct); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

It was decided to add another button that leads to another Activity window. How to add a button2 ?
While the transition is working only on one button. I can not understand how to register for the second.

I tried this:

 Button swith = (Button)findViewById(R.id.button1); Button swith2 = (Button)findViewById(R.id.button2); swith.setOnClickListener(new OnClickListener(); swith2.setOnClickListener(new OnClickListener() 

But does not work.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

as well as you do for the first button and for the second

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); Button swith2 = (Button)findViewById(R.id.button2); swith.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent SecAct = new Intent(MainActivity.this, SecondActivity.class); startActivity(SecAct); } }); swith2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent threActivity = new Intent(MainActivity.this, ThirdActivity.class); startActivity(threActivity); } }); Button swith3 = (Button)findViewById(R.id.button3); swith3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent ativity4 = new Intent(MainActivity.this, Activity4.class); startActivity(ativity4); } }); } 
  • when you add swith3.setOnClickListener (new View.OnClickListener () {starts swearing though the code Button swith = (Button) findViewById (R.id.button1); Button swith2 = (Button) findViewById (R.id.button2); swith.setOnClickListener (new View.OnClickListener () {@Override public void onClick (View v) {Intent SecAct = new Intent (MainActivity.this, SecondActivity.class); startActivity (SecAct);}}); - aivengos
  • how swears? Adding switch3, everything is fine - Yury Pashkov

when adding swith3.setOnClickListener (new View.OnClickListener () {starts to swear though the code

 Button swith = (Button)findViewById(R.id.button1); Button swith2 = (Button)findViewById(R.id.button2); swith.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent SecAct = new Intent(MainActivity.this, SecondActivity.class); startActivity(SecAct); } }); 

I took it with a bang ... maybe you can do without swith2.setOnClickListener(new View.OnClickListener() { without it, I now have the code like this but I didn’t check the performance ... although the debugger does not swear the code here:

 import android.os.Bundle; 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 MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); Button swith3 = (Button)findViewById(R.id.button3); swith.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent SecAct = new Intent(getApplicationContext(), SecondActivity.class); startActivity(SecAct); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

    I rummaged through the code, it seemed to work for everyone ... current trouble opens 1 button for the activation window 1 and button 2 opens the same activation window for 1 ... can anyone understand what the matter will tell? here is the code for the moment import android.os.Bundle; 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 MainActivity extends Activity {

     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button swith = (Button)findViewById(R.id.button1); Button swith3 = (Button)findViewById(R.id.button3); swith.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent SecAct = new Intent(getApplicationContext(), SecondActivity.class); Intent SecAct2 = new Intent(getApplicationContext(), SecondActivity2.class); startActivity(SecAct); startActivity(SecAct2); } }); swith3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent threActivity = new Intent(MainActivity.this, SecondActivity2.class); startActivity(threActivity); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }