Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

android - How to do multiple intents?

I'm trying to do 3 intents to start a new activity, however I find that I always get an error. I'm putting my code onto main.java code:

public class Main extends Activity {
Button service;
Button gallery;
Button contact;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    service = (Button)findViewById(R.id.Services);
    service.setOnClickListener(new View.OnClickListener() {
    });
    gallery = (Button)findViewById(R.id.Gallery);
    gallery.setOnClickListener(new View.OnClickListener() {
    }); 
    contact = (Button)findViewById(R.id.Contact);
    contact.setOnClickListener(new View.OnClickListener() {
    });


        public void onClick (View v) {
    // Inflate the menu; this adds items to the action bar if it is present.
    Intent intent = new Intent (Main.this, servicesActivity.class);
startActivity(intent);
        }
public void onClick1 (View v) {
    Intent intent1 = new Intent (Main.this, galleryActivity.class);
startActivity(intent1);
}
public void onClick2 (View v) {
    Intent intent2 = new Intent (Main.this, contactActivity.class);
startActivity(intent2);
        }

}
}

I have tried different ways and even putting brackets in different places. also I have been searching on the internet for weeks and found nothing that work with more than 1. I'm getting errors on OnClick and new View.OnClickListener(). I have got one working which is why I tried the same code with 3 buttons. Basically I have 3 buttons on main activity.xml. All I want to do is:

>'button1 >goes> activity1'
>'button2 >goes> activity2'
>'button3 >goes> activity3'

Please give me any hints or tips as I'm new to Android dev.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do all of them inside the same listener. Set all the listeners this way

service.setOnClickListener(this);
gallery.setOnClickListener(this);

then use one function and check the id of the View that was clicked

public void onClick2 (View v) {
Intent intent = new Intent();
switch (v.getId())  // get the id of the Button clicked
{
   case (R.id.Services):
      intent = new Intent(Main.this, servicesActivity.class);
      break;
   case (R.id.Gallery):
       intent = new Intent(Main.this, galleryActivity.class);
       break;
...
}
startActivity(intent);

You can actually clean it up even more to not repeat variables with something like this

public void onClick(View v)
{
    Intent intent = new Intent();  // create an Intent
        String act = null;                     // name for Activity to start with Intent
        String shield = "com.your.package.";  // set package name
switch (v.getId())   // get the id of the Button clicked
{
   case (R.id.Services):
      act = package + "Services";  // if Services button clicked use Services as the activity
      break;
   case (R.id.Gallery):
       act = package + "GalleryActivity";
       break;
...
}
try 
{
intent = new Intent(Main.this, Class.forName(act));  // create your Intent by changing your String act to a class name
startActivity(intent);  // start the Intent as normal
}
catch (ClassNotFoundException e){   // don't forget to catch invalid class names
e.printsStackTrace();
}

And as dymeh pointed out, make sure your Activity implements OnClickListener

This could probably be cleaned up a little more and may look more difficult but I use something like this in a custom menu and other places and it works nicely. It cuts down on separate functions and creating separate Intents. If you have to add something later or want to reuse the code it makes it a little easier, IMHO


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...