어떻게하면 이미지를 클릭 할 수있게 할 수 있습니까? 나는 몇 가지 방법을 시도했지만 성공하지는 못했다. 여기 내가 시도한 마지막 코드입니다 (이 클릭 할 수 있지만 얻을 오류) : 클릭 가능 이미지 - android
<ImageView
android:src="@drawable/tbnewbutton"
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
이 코드를 실행, 그리고 난이 오류가 이미지를 클릭 :
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff
}
});
이곳은 XML의 일부입니다 :
: 여기01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
는 솔루션의
는 XML :
<ImageButton
android:src="@drawable/tbnewbutton"
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@null" />
코드 :
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
좋아,이 하나의 작품,하지만 내 이미지는 버튼 안에 있습니다. 그래서 내 레이아웃 제동이야. 이 문제를 해결하려면 android : background = "@ null"을 XML에 추가하면됩니다. – user484146
그 답을 도움이 될만한 것으로 표시 하시겠습니까? –
사용할 올바른 배경은'android : background = "? android : selectableItemBackground"'입니다. – thirtythreeforty