如何在片段内显示AlertDialog

时间:2018-09-18 16:05:56

标签: android android-studio

我正在尝试创建一个程序,当用户单击Map中存在的Marker时,该程序将显示AlertDialog对话框(Map将显示为片段)。 MarkerClick事件在我的代码中运行良好。 但这不是触发对话框。

public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(getContext());
    mMap = googleMap;
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(new LatLng(33.8688, 151.2093)));
    CameraPosition sidney = CameraPosition.builder().target(new LatLng(33.8688, 151.2093)).zoom(16).bearing(0).tilt(45).build();
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(sidney));
    my_location();//calling function for current location button



    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {//Marker click will be trigered here

        @Override
        public boolean onMarkerClick(Marker marker) {
            Toast.makeText(getActivity(),"Location permission required for fetching your current location Permission>Enable location",Toast.LENGTH_LONG).show();


            AlertDialog.Builder alert_alarm=new AlertDialog.Builder(getActivity());
            alert_alarm.setMessage("Do you want to set alarm here?").setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });



            return false;
        }
    });

}

0 个答案:

没有答案