我有一個main_view.xml
其中包含ImageButton
其中的按鈕,當我點擊/觸摸按鈕,ButtonListener
函數永遠不會被調用。無法點擊ImageButton
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/mapArea"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/locate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="@string/findingLocation"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageButton
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="@dimen/rightLeftMargin"
android:layout_marginLeft="@dimen/rightLeftMargin"
android:background="@null"
android:clickable="true"
android:contentDescription="@string/button1text"
android:src="@drawable/ic_feed_active" />
<ImageButton
android:id="@+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/rightLeftMargin"
android:background="@null"
android:contentDescription="@string/button2"
android:src="@drawable/ic_button2" />
<ImageButton
android:id="@+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="@dimen/rightLeftMargin"
android:layout_marginRight="@dimen/rightLeftMargin"
android:background="@null"
android:contentDescription="@string/button3View"
android:src="@drawable/ic_settings_active" />
</RelativeLayout>
,並按照MainActivity.Java代碼 公共類MainActivity擴展活動{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User usrObj = new User();
if (usrObj.authenticateUser())
setContentView(R.layout.main_view);
else
setContentView(R.layout.login);
}
public void ButtonListener() {
ImageButton oggleButton = (ImageButton) findViewById(R.id.Button1);
ToggleButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "ImageButton is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
}
請幫助。