我有非常简单的代码,其中包含 ImageButton 和 OnClickListener 指向另一个 Activity,单击 ImageButton 不会触发 onClick(同样的问题是 Button):
public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
this.btnCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tools, menu);
return true;
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageButton
android:id="@+id/cameraButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/btncamera"
android:contentDescription="@string/desc" />