How to do multiple intents?

后端 未结 2 1402
走了就别回头了
走了就别回头了 2020-12-10 09:49

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 e         


        
相关标签:
2条回答
  • 2020-12-10 10:29

    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

    0 讨论(0)
  • 2020-12-10 10:33
    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() {
            @Override
            public void onClick(View v)
            {
    
                Intent intent = new Intent (Main.this, servicesActivity.class);
                startActivity(intent);
            }
    
        });
        gallery = (Button)findViewById(R.id.Gallery);
        gallery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent1 = new Intent (Main.this, galleryActivity.class);
                    startActivity(intent1);
    
            }
    
        }); 
        contact = (Button)findViewById(R.id.Contact);
        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent2 = new Intent (Main.this, contactActivity.class);
                      startActivity(intent2);
    
            }
    
        });
    
    }
    
    0 讨论(0)
提交回复
热议问题