2

我試圖讓一個按鈕在SupportMapFragment的頂部工作,但OnclickListener沒有觸發。 這裏是我的代碼:OnClickListener不能使用谷歌地圖頂部的按鈕片段

MapFragment:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) { 
    super.onCreateView(inflater, container, savedInstanceState); 
    setRetainInstance(true); 
    SP = getActivity().getSharedPreferences(SHARED_PREFS_NAME, 0); 
    this.SP.registerOnSharedPreferenceChangeListener(this); 
    View view = inflater.inflate(R.layout.map, container, false); 

    FrameLayout frameLayout = new FrameLayout(getActivity()); 
    frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
    ((ViewGroup) view).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT)); 

    setHasOptionsMenu(true); 
    tracker = GoogleAnalyticsTracker.getInstance(); 
    setMapTransparent((ViewGroup) view); 
    String mapType = SP.getString("PREF_MAP", "MAP_TYPE_NORMAL"); 

    FragmentManager myFragmentManager = getFragmentManager(); 
    SupportMapFragment mySupportMapFragment = (SupportMapFragment) myFragmentManager.findFragmentById(R.id.map); 
    myMap = mySupportMapFragment.getMap(); 
    if (mapType.equals("MAP_TYPE_NORMAL")) { 
     myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
    } else if (mapType.equals("MAP_TYPE_SATELLITE")) { 
     myMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
    } else if (mapType.equals("MAP_TYPE_HYBRID")) { 
     myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    } 
    myMap.setMyLocationEnabled(true); 
    myMap.setOnMapClickListener(this); 
    myMap.setOnMapLongClickListener(this); 
    myMap.getUiSettings().setZoomControlsEnabled(true); 
    myMap.getUiSettings().setMyLocationButtonEnabled(true); 
    myMap.getUiSettings().setScrollGesturesEnabled(true); 
    myMap.getUiSettings().setZoomGesturesEnabled(true); 
    ((Activity) getActivity()).setSupportProgressBarIndeterminateVisibility(false); 

    button = (Button) view.findViewById(R.id.sbutton); 
    this.button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(getActivity(), "CLICK!", Toast.LENGTH_SHORT).show(); 

     } 

    }); 

    return view; 
} 

map.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:holo="http://schemas.android.com/apk/res-auto" 
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".Main" 
> 

<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment" 
    /> 

<Button 
    android:id="@+id/sbutton" 
    android:layout_width="48dp" 
    android:layout_height="48dp" 
    android:gravity="center_horizontal|center_vertical"   
    android:background="@drawable/ic_social_share_light" 
     /> 

不知道爲什麼不工作。 有人可以看看代碼並告訴我我做錯了什麼嗎?

謝謝

+0

問題是你已經張貼了很多不相關的代碼。如果您需要一些幫助,請提供SSCCE.org。 –

回答

1

我已經解決了這個問題。 我已經將onClick添加到了我的map.xml中的按鈕,而不是使用MapFragment中的代碼,而是使用了我的MapActivity中的代碼,如下所示。 這樣我的按鈕工作正常。

Map.xml

android:onClick="sButton" 

MapActivity

public void sButton(View v) { 
    Toast.makeText(this, "CLICK!", Toast.LENGTH_SHORT).show(); 
} 
+0

很棒找到邦德先生!非常感謝你!任何想法爲什麼onclicklistener在這種情況下不起作用? – Price

0

你被添加到上面的framelayout。刪除這個:

FrameLayout frameLayout = new FrameLayout(getActivity()); 
    frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
    ((ViewGroup) view).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 
     LayoutParams.MATCH_PARENT)); 

setMapTransparent((ViewGroup) view); 
+0

仍然無法正常工作。 – bond

+0

這不應該是理由,但嘗試刪除setMapTransparent。 – Devrim

+0

是的,已經嘗試過了。還是行不通。 – bond