Java 如何在android中创建图像按钮?

Java 如何在android中创建图像按钮?,java,android,button,android-activity,imagebutton,Java,Android,Button,Android Activity,Imagebutton,所以我是android开发新手…我如何创建一个像按钮一样的图像,所以当我按下该图像时,图像开始一个特定的活动。 所以我想让它显示为图像: <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layo

所以我是android开发新手…我如何创建一个像按钮一样的图像,所以当我按下该图像时,图像开始一个特定的活动。 所以我想让它显示为图像:

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="33dp"
    android:text="Button" />

使用ImageView元素,将单击侦听器附加到该元素

XML:

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myPic" 
    />
您还可以使用ImageButton(以相同的方式)。这几乎没有什么区别。你可以在这里看到更多细节

将ImageButton创建为:

在main.xml中:

<ImageButton android:id="@+id/ib"
    android:src="@drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
    }
    private OnClickListener ibLis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //START YOUR ACTIVITY HERE AS
             Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
             startActivity(intent, 0);
        }
    };
编辑:

<ImageButton android:id="@+id/ib"
    android:src="@drawable/bookmark" <-- SET BUTTON IMAGE HERE -->
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
ImageButton ib=(ImageButton)findViewById(R.id.ib);
ib.setOnClickListener(ibLis);
    }
    private OnClickListener ibLis=new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            //START YOUR ACTIVITY HERE AS
             Intent intent = new Intent(YOUR_CURRENT_ACTIVITY.this,NextActivity.class);
             startActivity(intent, 0);
        }
    };
第二个选项,如果要使用“按钮视图”创建类似图像的按钮,请创建自定义按钮,如下所示:

首先在res/drawable文件夹中放置所有图像,如按下、聚焦和默认图像,然后在drawable/newbtn.xml中添加一个newbtn.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_pressed="true"  
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->  
    <item android:state_focused="true"  
          android:drawable="@drawable/button_focused" /> <!-- focused -->  
    <item android:drawable="@drawable/button_normal" /> <!-- default -->  
</selector>
有关使用图像创建自定义按钮的信息,请参见本教程


使用“setOnClickListener()”过于复杂了。而是在XML中使用“onClick”属性:

<ImageButton
    android:id="@+id/button19"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:onClick="yourCallback"
    android:src="@drawable/your_image"
/>

public void yourCallback(View view) 
{
    ...
}

公共无效回调(视图)
{
...
}

你是说一个ImageButton在Android中有一个专门用于此的控件。发布您的问题之前,请进行谷歌搜索。@user1414682:确保您在清单中注册了第二个活动,因为我发现了问题:我在代码中有android:text=“Button”和android:src=“@drawable/bookmark”,所以我删除了“text”谢谢!还有一个问题,我可以使用动画图像吗?您可以使用
AnimationDrawable
为图像按钮设置动画,但我不确定。所以只要在谷歌上搜索一个
nimationDrawable
,也许这会对你有所帮助。谢谢。