我创建了一个简单的应用程序,其中有一个图像按钮。
我希望在单击图像按钮后干杯。
我的问题是,我可以看到那个图像按钮,但是当我点击那个图像按钮时没有任何反应。我找不到该代码的任何问题。 这是我的代码。
这是我的xml文件
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:src="@drawable/myimage"/>
</RelativeLayout>
ContentMain.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class ContentMain extends AppCompatActivity {
ImageButton imgButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
imgButton =(ImageButton)findViewById(R.id.imageButton);
imgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"You download is resumed",Toast.LENGTH_LONG).show();
}
});
}
}
最佳答案
你应该先调试自己
你可以这样使用
将此添加到您的 Xml 中
<ImageButton
android:clickable="true"
android:onClick="myClick"
/>
然后
在 Java 类中
public void myClick(View v)
{
Toast.makeText(ContentMain.this,"You download is resumed",Toast.LENGTH_LONG).show();
}
清理-重建-运行
关于java - 图像按钮事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37485477/