2012-03-17 66 views
1

如何使图像在android中可点击? 我不想使用imageButton,因为它将图像放在按钮上。 我试着用下面的代码,但点击没有任何反应.. 在XML:使图像在Android中可点击2

<ImageView 
android:id="@+id/arrow1" 
android:src="@drawable/arrow" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_x="12dp" 
android:layout_y="90dp" 
android:clickable="true" 
android:onClick="onClick"> 
</ImageView> 

中的.java:

final ImageView diskView4 = (ImageView) findViewById(R.id.arrow1); 


diskView4.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v){ 
     //my codes 
    } 
}); 
+0

安卓的onClick = “点击” 删除此行... – 2012-03-17 10:24:35

+0

更改名称的onClick在布局xml文件中。像java中的android:onClick =“imageClick”public void imageClick(View v)不能使用onClick()方法的名字。 – user370305 2012-03-17 10:37:40

回答

3

不需要增加额外的属性

<ImageView 
android:id="@+id/arrow1" 
android:src="@drawable/arrow" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_x="12dp" 
android:layout_y="90dp" /> 

这会做,你的代码看起来没问题。跟踪您的点击

diskView4.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v){ 
     System.out.println("image clicked...");//check logcat 
    } 
}); 
+0

谢谢.. logcat在哪里? – sumit 2012-03-17 10:32:50

+0

请参阅http://developer.android.com/guide/developing/debugging/ddms。html打开logcat选项卡,你可以看到你的系统消息。 – 2012-03-17 10:36:39

+0

是只适用于linux?因为我正在使用Windows .. – sumit 2012-03-17 10:46:31

2

将imagebutton的背景设置为透明,或者更好地设置为android.R.attr.selectableItemBackground。那么你甚至有一个像标准无边界按钮一样的onClick效果。

<ImageButton 
      android:background="?android:attr/selectableItemBackground" 
      android:src="@drawable/yourimage "/> 
1

使用android:onClick="doSomething" XML标记非常简单。

在已充气的布局(通常是一个活动),您必须声明函数像

public void doSomething(View view)

你可以在这里找到文档

http://developer.android.com/reference/android/view/View.html#attr_android:onClick

该信息的上下文

通过这种方式,如果图像onClick不是动态的,你会得到一个更清洁的活动。

编辑

例,在XML布局

<ImageView 
    android:id="@+id/image_id" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:src="@drawable/image" 
    android:onClick="doSomething" /> 

在活动,如果你想开例如

class ImageActivity extend Activity{ 
    @Override 
    public void onCreate(Bundle params){ //... } 

    public void doSomething(View view){ 
     startActivity(new Intent(this, AnotherActivity.class)); 
    } 
} 

一个新的Activity在你的View元素点击的视图,所以你可以识别哪个元素触发了该功能。

public void doSomething(View view){ 
    if(view.getId() == R.id.image_id) 
     startActivity(new Intent(this, AnotherActivity.class)); 
    else 
     Log.i("Image", "Try to open the Activity from another view different from ImageView") 
} 
1

如果上面的解决方案不适合你,那么你可以试试这个。

main.xml中

<ImageView 
     android:id="@+id/img_call_icon"   
     android:layout_width="40px" 
     android:layout_height="40px" 
     android:clickable="true" 
     android:onClick="onClickHandler"/> 

而且在MyActivity.java

public void onClickHandler (View v) 
    { 
     switch (v.getId()) { 
     case R.id.img_call_icon: 

     // put your onclick code here 
    } 
} 

谢谢...

+0

谢谢,它显然是一个很好的方法来做到这一点。 。 – sumit 2012-03-17 17:03:36