Java imageButton设置ClickListener
我有一个xml文件。我正在为图像按钮btnSeat1、btnSeat2、btnSeat3、btnSeat4设置OnClickListener。但当我按下按钮时,什么也没发生。我已经尝试过设置clickable,但没有成功Java imageButton设置ClickListener,java,android,imagebutton,Java,Android,Imagebutton,我有一个xml文件。我正在为图像按钮btnSeat1、btnSeat2、btnSeat3、btnSeat4设置OnClickListener。但当我按下按钮时,什么也没发生。我已经尝试过设置clickable,但没有成功 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapActivity" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.google.android.gms.maps.SupportMapFragment"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<Button
android:id="@+id/btnFilters"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/filter"
android:onClick="filters_onclick"
android:text="Мои Фильтры" />
<Button
android:id="@+id/btnCabinet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="1dp"
android:layout_toRightOf="@id/btnFilters"
android:background="@drawable/kabinet"
android:onClick="cabinet_onclick"
android:text="Мой Кабинет" />
<ImageButton
android:id="@+id/btnSettings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnCabinet"
android:layout_alignParentTop="true"
android:layout_marginLeft="1dp"
android:layout_toRightOf="@id/btnCabinet"
android:background="@drawable/set"
android:onClick="settings_onclick" />
<LinearLayout
android:id="@+id/linearShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/btnCabinet"
android:background="#c7c8c8"
android:orientation="horizontal" >
<TextView
android:id="@+id/textfBelowMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#c7c8c8"
android:text="Быстрый фильтр"
android:textColor="@color/Black"
android:textSize="14dp" />
<ImageButton
android:id="@+id/btnMapFilter"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/image_open"
android:onClick="map_filter" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnCabinet"
android:layout_toRightOf="@id/linearShow"
android:background="#c7c8c8"
android:orientation="horizontal" >
<TextView
android:id="@+id/textBelowM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#c7c8c8"
android:text="Заявок: "
android:textColor="@color/Black"
android:textSize="14dp" />
<ImageButton
android:id="@+id/btnMapRequests"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/image_open"
android:onClick="request_filter" />
</LinearLayout>
<ImageButton
android:id="@+id/btnSeat1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/btnCabinet"
android:background="@drawable/seat1" />
<ImageButton
android:id="@+id/btnSeat2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="@id/btnSeat1"
android:layout_marginLeft="1dp"
android:background="@drawable/seat2"/>
<ImageButton
android:id="@+id/btnSeat3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="@id/btnSeat2"
android:layout_marginLeft="1dp"
android:background="@drawable/seat3" />
<ImageButton
android:id="@+id/btnSeat4"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="@id/btnSeat3"
android:layout_marginLeft="1dp"
android:background="@drawable/seat4" />
<ImageButton
android:id="@+id/btnRefresh"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/refresh"
android:onClick="refresh_onclick" />
<ImageButton
android:id="@+id/btnGo"
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="1dp"
android:layout_toRightOf="@id/btnRefresh"
android:background="@drawable/go"
android:onClick="go_onclick" />
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textBelowMap"
android:layout_toRightOf="@+id/btnGo"
android:layout_marginRight="50dp"
android:layout_marginLeft="5dp"
android:background="@drawable/after_update_backgound"
android:text="Отображать только вашего попутчика"
android:textSize="12dp" />
<Button
android:id="@+id/btnFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="90dp"
android:layout_marginTop="20dp"
android:text="Конец поездки"
android:textSize="12dp" />
<TextView
android:id="@+id/textBelowMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnGo"
android:layout_centerHorizontal="true"
android:text="Поиск без фильтра осуществляется в радиусе 5км от локации"
android:textColor="@color/Black"
android:textSize="8dp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
我是这样实例化的
btnSeat1 = (ImageButton) findViewById(R.id.btnSeat1);
btnSeat2 = (ImageButton) findViewById(R.id.btnSeat2);
btnSeat3 = (ImageButton) findViewById(R.id.btnSeat3);
btnSeat4 = (ImageButton) findViewById(R.id.btnSeat4);
我确信这是对的,因为我可以在按钮上设置图像
当我说什么都没发生时,我是说这个。它没有崩溃,只是不起作用。
我用调试器检查了它,但是无法到达OnClick中的断点
有人说框架布局可能是个问题,因为也许这个按钮位于第二层,也许这就是问题所在,但我不知道如何处理它你能描述一下“但它不起作用”吗?它会崩溃吗,绝对不会发生任何事情,等等…你能在Java中声明和定义变量btnSeat1吗?展示一下你是如何实例化btnSeat1的?还有..你的代码中有一些错误。@codeMagic,我错过了。。
btnSeat1 = (ImageButton) findViewById(R.id.btnSeat1);
btnSeat2 = (ImageButton) findViewById(R.id.btnSeat2);
btnSeat3 = (ImageButton) findViewById(R.id.btnSeat3);
btnSeat4 = (ImageButton) findViewById(R.id.btnSeat4);