画像をクリック可能にするにはどうすればよいですか?私はいくつかの方法を試みたが、成功しなかった。ここ は、私が試した最後のコードは、(それがクリック可能ですが、エラーを取得します)です:Clickable image - android
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff
}
});
、ここでは、XMLからの一部です:
<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" />
このコードを実行している、と私はこのエラーを取得した画像をクリックすると: HERE
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"という行を追加して、すべての作品を完璧にしてください! – user484146
その答えを参考にしてもらえますか? –
正しい背景は 'android:background ="?android:selectableItemBackground "'です。 – thirtythreeforty