简体   繁体   English

Android ImageButton 导航活动

[英]Android ImageButton to navigate through activities

I tried using ImageButton as a regular button to navigate from activity to activity.我尝试使用 ImageButton 作为常规按钮从活动导航到活动。 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.我有一个包含 6 个图像按钮的活动,我希望让用户在单击任何图像按钮时转到他们适当的活动。 This however, does not work.然而,这不起作用。 Can anyone guide me as to how to accomplish this?谁能指导我如何做到这一点?

This is my xml file:这是我的 xml 文件:

<?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:这是我的 .java 文件的样子:

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请检查您的 XML 页面图像按钮 ID 并给它不同的 ID..然后把你的意图代码......并定义你的第二个类并将其提供给 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. PS不要忘记在清单文件中添加您的PracticeActivity活动。
Importantly check your other activity.重要的是检查您的其他活动。 If there is some thing which causes runtime error in oncreate then also it wont work.如果有一些事情导致oncreate运行时错误,那么它也不会工作。

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...然后对另一个图像按钮使用相同的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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