我非常確定一切正常,但是當我點擊任何圖像時,日誌並未顯示在Eclipse的控制檯區域中。OnClickListener不適用於ImageView
最終,我想要在點擊這些圖片時出現彈出式菜單,但現在我只想讓onclick事件正常工作,然後在稍後添加這些事件。
我的代碼:
的Java
public class SecondScreen extends HelloAndroid {
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
// initialize header
ImageView header = (ImageView) findViewById(R.id.header_image);
header.setOnClickListener(headerClick);
// Make sure vitals and details are same size
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
params.weight = 0.5f;
// initialize vitals image
ImageView vitals = (ImageView) findViewById(R.id.vitalsigns_image);
vitals.setLayoutParams(params);
vitals.setOnClickListener(vitalsClick);
// initialize details image
ImageView details = (ImageView) findViewById(R.id.details_image);
details.setLayoutParams(params);
details.setOnClickListener(detailsClick);
}
private OnClickListener headerClick = new OnClickListener() {
public void onClick(View view) {
Log.d("Clicked", "Header Clicked!");
}
};
private OnClickListener vitalsClick = new OnClickListener() {
public void onClick(View view) {
Log.d("Clicked", "Vitals Clicked!");
}
};
private OnClickListener detailsClick = new OnClickListener() {
public void onClick(View view) {
Log.d("Clicked", "Details Click!");
}
};
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- HEADER -->
<ImageView
android:id="@+id/header_image"
android:src="@drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<!-- FOOTER -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_alignParentBottom="true">
>
<ImageView
android:id="@+id/vitalsigns_image"
android:src="@drawable/vitalsigns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="@+id/details_image"
android:src="@drawable/details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_alignParentBottom="true"
/>
</LinearLayout>
我在做什麼錯?
爲什麼你的代碼中太多空白空間..? – Siddharth 2015-08-26 05:36:30