简体   繁体   中英

Android ImageButton to navigate through activities

I tried using ImageButton as a regular button to navigate from activity to activity. I have this activity containing 6 imagebuttons and I wish to have thee user go to their appropriate activity on click on any of the image buttons. This however, does not work. Can anyone guide me as to how to accomplish this?

This is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/book_shelf"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center"
        android:text="WELCOME"
        android:textSize="40dip" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="102dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/radioButton2"
        android:layout_alignBottom="@+id/radioButton2"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="23dp"
        android:text="Autoplay" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="33dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:text="Read by self" />

    <ImageButton
        android:id="@+id/ImageButton01"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_alignTop="@+id/imageButton1"
        android:layout_marginLeft="15dp"
        android:src="@drawable/clown_icon" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_below="@+id/radioButton2"
        android:layout_marginTop="34dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:src="@drawable/nurse_icon" />

    <ImageButton
        android:id="@+id/ImageButton03"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_below="@+id/imageButton1"
        android:layout_marginTop="28dp"
        android:layout_toRightOf="@+id/radioButton1"
        android:src="@drawable/police_w_icon" />

    <ImageButton
        android:id="@+id/ImageButton02"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton01"
        android:layout_alignTop="@+id/ImageButton03"
        android:src="@drawable/m_girl_icon" />

    <ImageButton
        android:id="@+id/ImageButton04"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton02"
        android:layout_alignTop="@+id/ImageButton05"
        android:src="@drawable/firefighter_icon" />

    <ImageButton
        android:id="@+id/ImageButton05"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:layout_alignLeft="@+id/ImageButton03"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="36dp"
        android:src="@drawable/chef_w_icon" />

</RelativeLayout>

This is what my .java file looks like:

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


public class GirlMenuActivity extends Activity {
    ImageButton imagebtn1;
    ImageButton imagebtn2;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_girl);
        
        addListenerOnButton();
    }
    
    public void addListenerOnButton() {
        imagebtn1 = (ImageButton) findViewById(R.id.ImageButton01);
        imagebtn1.setOnClickListener(new OnClickListener() {
             
            @Override
            public void onClick(View arg0) {
 
                Intent intent = new Intent
                        (getApplicationContext(), PracticeActivity.class);
                    startActivity(intent); 
            }
        });
    }
}

As you can see only one imagebutton has been implemented yet. It doesn't work.

plz check your XML page image button id and give it the different id..and then put your intent code ...and define also ur second class and give its to the manifest.xml

  Intent intent = new Intent(MainActivity.this, second.class);
    startActivity(intent);

Try this code:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_girl);

         imagebtn1 = (ImageButton) findViewById(R.id.imageButton1);
        imagebtn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent
                        (GirlMenuActivity.this, PracticeActivity.class);
                    startActivity(intent); 
            }
        });
    }


}

PS : Dont forget to add your PracticeActivity in your manifest file.
Importantly check your other activity. If there is some thing which causes runtime error in oncreate then also it wont work.

try this code.hope it helps..

imageButton1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent i = new Intent
                    (MainActivity.this, SecondActivity.class);
                startActivity(i); 
        }
    });
}

then use same for another image buttons...

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