简体   繁体   中英

starting another activity from an imagebutton

So I want to start an activity based on which imagebutton the user clicks.
Currently I have something like this:

private void LoadDrawer() {
    // TODO Auto-generated method stub
    ImageButton Home, Icon, Ranking, History;
    Home = (ImageButton) findViewById(R.id.ibHome);
    Icon = (ImageButton) findViewById(R.id.ibIcon);
    Ranking = (ImageButton) findViewById(R.id.ibRanking);
    History = (ImageButton) findViewById(R.id.ibHistory);
    Home.setOnClickListener(this);
    Icon.setOnClickListener(this);
    Ranking.setOnClickListener(this);
    History.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
        case R.id.ibHome:
            ClassLoader App2;
            Intent intent2 = new Intent(this, App2.class);
            break;
        case R.id.ibIcon:
            Intent intent1 = new Intent(this, App1.class);
            break;
        case R.id.ibRanking:
            Intent intent4 = new Intent(this, App4.class);
            break;
        case R.id.ibHistory:
            Intent intent5 = new Intent(this, App5.class);
            break;
    }
}

Now the reason you see intent there, is because I've tried using intent to start the other activity(s) but to no avail...

Now I retrieve the intent this way:

Intent intent[number] = getIntent();
String message = intent[number].getAction();

It doesn't actually stay number there, it's just what I have, for example I have 2 here because this is from app2

I don't quite understand what your problem is. It should be as simple as this...

@Override
public void onClick(View v) {
    Intent i = null;

    switch (v.getId()) {
    case R.id.ibHome:
        i = new Intent(this, App2.class);
        break;
    case R.id.ibIcon:
        i = new Intent(this, App1.class);
        break;
    case R.id.ibRanking:
        i = new Intent(this, App4.class);
        break;
    case R.id.ibHistory:
        i = new Intent(this, App5.class);
        break;
    }
    if (i != null)
        startActivity(i);
}

You can use something like this too:

Home= (ImageButton) findViewById(R.id.ibHome);
Home.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this, App2.class);
                startActivity(i);
            }
        });
Icon= (ImageButton) findViewById(R.id.ibIcon);
Icon.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i = new Intent(MainActivity.this, App1.class);
                    startActivity(i);
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM