-1

I am working on a school project and I need to start multiple activities with buttons (4 to be exact). I have an idea but it works for one button, when I try to implement the second button to start the activity I get lot of errors. Later I read that I can't use multiple onClick(View) in one activity. Anyway, here is my idea, I think I know why it does not work, but I have nothing else, and I can't find nothing in my books.

MainActivity.java

package com.example.skvik.unit_conv;

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

public class MainActivity extends Activity implements View.OnClickListener {
    Button getVolt, getamp;

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

    private void setUpUI() {
        this.getVolt = (Button) findViewById(R.id.volt);
        this.getVolt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent explicitIntent = new Intent();
        explicitIntent.setClass(getApplicationContext(), Napon.class);
        this.startActivity(explicitIntent);
    }

    private void setUpUI() {
        this.getamp = (Button) findViewById(R.id.amp);
        this.getamp.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent explicitIntent = new Intent();
        explicitIntent.setClass(getApplicationContext(), struja.class);
        this.startActivity(explicitIntent);
    }
}

Any idea will help :) This is my idea for two buttons, and I know this will not cut it, but I hit a dead end. Thanks for the help.

badman
  • 5
  • 1

1 Answers1

1

Please try this

 @Override
public void onClick(View v) {
  switch(v.getId){
   case R.id.button1:
    Intent explicitIntent = new Intent();
    explicitIntent.setClass(getApplicationContext(), struja.class);
    this.startActivity(explicitIntent);
    break;
    case R.id.button2:
    //do what you want to do for button2
    break;
    case R.id.button3:
    //do what you want to do for button3
    break;
    case R.id.button4:
    //do what you want to do for button4
    break;


}}
Constantin N.
  • 2,739
  • 2
  • 16
  • 27
  • 1
    Code only answers are generally not considered good. It's better to add some description as to what is happening, what is different about your code, and why it provides a solution to the problem. – codeMagic Apr 21 '18 at 19:36