Android button navigation with onclick listeners

2019-09-07 09:09发布

main.xml
--------
|button1| (button press)>page1.xml
|button2| (button press)>page2.xml
|button3| (button press)>page3.xml
|button4| (button press)>page4.xml
|button5| (button press)>page5.xml
|button6| (button press)>page6.xml
-------------------------------------------

When a button is clicked it goes to a corresponding page. What is the best/easiest way to implement this? I have tried everything to no avail when I try onclick listeners. I can get one listener to work but when I try to get more than one it messes up and I can't figure it out. My current method is using these 2 java files
activity1.java

package install.fineline;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class Activity1 extends Activity {

Button Button1;
Button Button2;
Button Button3;
Button Button4;
Button Button5;
Button Button6;



public void addListenerOnButton() {

final Context context = this;

Button1 = (Button) findViewById(R.id.autobody);

Button1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button2 = (Button) findViewById(R.id.glass);

Button2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button3 = (Button) findViewById(R.id.wheels);

Button3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button4 = (Button) findViewById(R.id.speedy);

Button4.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button5 = (Button) findViewById(R.id.sevan);

Button5.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Intent intent = new Intent(context, Activity2.class);
        startActivity(intent);   

    }

});

Button6 = (Button) findViewById(R.id.towing);

Button6.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

    Intent intent = new Intent(context, Activity2.class);
    startActivity(intent);   

}

});

}}

and here is my other java file

activity2.java
package install.fineline;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Activity2 extends Activity {

Button button1;

public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autobody);
}
Button button2;

public void onCreate2(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.glass);
}
Button button3;

public void onCreate3(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wheels);
}
Button button4;

public void onCreate4(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.speedy);
}
Button button5;

public void onCreate5(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sevan);
}

Button button6;

public void onCreate6(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.towing);
}}

6条回答
趁早两清
2楼-- · 2019-09-07 09:10
  1. Change your OnClick methods with a single switch-case
  2. Pass data from the activity1 like this:

    intent.putExtra("buttonClick", buttonnumber); startActivity(intent);

  3. Receive the data in activity2 like this:

    clickedButton = getIntent().getExtras().getString("buttonClick");

  4. Change the method name to OnCreate in activity2

查看更多
Lonely孤独者°
3楼-- · 2019-09-07 09:12

Ok, first off, implement the OnClickListener, you'll have to add the unemplemented method onClick()

public class Activity1 extends Activity implements OnClickListener {

Then initialize your buttons and add click listeners to them in your onCreate()

Button1 = (Button) findViewById(R.id.autobody);
Button1.setOnClickListener(this);

Then in onClick() set up a switch/case

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.autobody:
        // do code
        break;
    case R.id.glass:
        // do code
        break;
    }
}

I just did a few of your buttons, but you can figure out the rest. Just make sure to initialize and add the onClickListeners to them all in onCreate()

查看更多
爷的心禁止访问
4楼-- · 2019-09-07 09:16

Do not use buttons for this functionality. Use a ListView and with its onItemClickListener you will get the number of the row that is clicked. You can then use ViewPager to show pages based on the click.

查看更多
仙女界的扛把子
5楼-- · 2019-09-07 09:24

First of all, this is not your problem, but makes the code way easier to read and preserves memory: Make a single onClickListener and check in there which button was clicked.

class Activity1 extends Activity implements onClickListener {

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            // handle button1 press
        case R.id.button2:
            // ...
        }
    }
}

Now to the real problem: In your second activity, Android does not know any onCreate1(), onCreate2() etc functions. Instead, you have to implement onCreate() (which Android does know), and send it the button that was pressed in the Intent, like this:

// You can just take your activity object as the context, 
// no need to convert explicitly.
Intent i = new Intent(this, Activity2.class);
i.putExtra("button", 1);
startActivity(i);

Then get the data in your activity:

public void onCreate() {
    int button = getIntent.getIntent().getInt("button");
}
查看更多
来,给爷笑一个
6楼-- · 2019-09-07 09:26

You should define one activity for each of the pages. So totally 7 activities including the main. In main activity, define single onClickListener to handle all clicks.

class ClickListener implements View.OnClickListener {
    void onClick(View v) {
       switch(v.getId()) {
        case R.id.btn_1:
             ...
             break;
        ...
       }
    }

Set this listener to all your 6 buttons.

查看更多
Rolldiameter
7楼-- · 2019-09-07 09:36

You are definitely doing this all wrong. All of your buttons are calling the same activity. If you want them to go different places, create different activities. If you want them to go the the same place with different data, use the intent to send the information as an extra.

Also, just for readability, you may want to learn about XML defined on-click methods.

查看更多
登录 后发表回答